0

I am writing an algorithm that should return "yes" if a list of customers are to pay for a movie and receive their change (money) considering the the clerk starts receiving payments with an empty cashier pause, meaning he cant give change from the start. The movie costs 25 $ so if a customer has 50 $ the clerk has to refuse unless he has already received 25 $ from previous customer so is able to use that as change for the next customer.

I have such algorithm

public static String Tickets(int[] peopleInLine) {
 int sumOfMoneyWithCashier = 0;
 int cost = 25;
 for (int i = 0; i < peopleInLine.length; i++) {
 if (peopleInLine[i] == cost) {
 sumOfMoneyWithCashier += peopleInLine[i];
 if (peopleInLine[i + 1] == cost) {
 sumOfMoneyWithCashier += cost;
 } else if (peopleInLine[i + 1] > cost) {
 int change = peopleInLine[i + 1] - cost;
 if (sumOfMoneyWithCashier >= change) {
 sumOfMoneyWithCashier -= change;
 } else {
 System.out.println("no");
 return "NO";
 }
 }
 } else if (peopleInLine[i] > cost) {
 int change = peopleInLine[i] - cost;
 if (sumOfMoneyWithCashier >= change) {
 sumOfMoneyWithCashier -= change;
 } else {
 System.out.println("no");
 return "NO";
 }
 }
 }
 System.out.println("YES");
 return "YES";
}

Now it works but not perfect , how can i improve this code so it check handles most of the scenario, ignore case where customer comes with less than 25. customers must come with multiples of 25 between range of {25, 50, 100} How can i make this code better is the question , i would appreciate an example code and explanation basically

Line.Tickets(new int[] {25, 25, 50}) // => YES 
Line.Tickets(new int[]{25, 100}) // => NO. Vasya will not have enough money to give change to 100 dollars
Line.Tickets(new int[] {25, 25, 50, 50, 100}) // => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50)
Fábio Nascimento
2,7371 gold badge23 silver badges29 bronze badges
asked Dec 12, 2018 at 13:02
5
  • How can I improve/make it better is a pretty vague question. If you have a specific situation when the program misbehaves or gives the wrong result than explain that situation (or situations) with example of given input and the result vs the expected result. Commented Dec 12, 2018 at 13:14
  • ok i got the task out of a codewars.com so there 12 tests it should pass , currently it passes 10 test , the problem is that codewars,com doesnt show you the tests inputs that were used @JoakimDanielson Commented Dec 12, 2018 at 13:18
  • (peopleInLine[i + 1] == cost) i am doing this to represent next element in the array @SamzSakerz Commented Dec 12, 2018 at 13:19
  • its just my way of getting next value in an array @SamzSakerz Commented Dec 12, 2018 at 13:28
  • may be not the best way @SamzSakerz Commented Dec 12, 2018 at 13:28

1 Answer 1

3

This is a simple solution to what you are trying to do see if it works out for you :)

public static String Tickets(int[] peopleInLine) {
 int d25 = 0, d50 = 0;
 for (int aPeopleInLine : peopleInLine) {
 if (aPeopleInLine == 25){
 d25++;
 }
 if (aPeopleInLine == 50) {
 d25--;
 d50++;
 }
 if (aPeopleInLine == 100) {
 if (d50 > 0) {
 d50--;
 d25--;
 } else {
 d25 -= 3;
 }
 }
 if (d25 < 0){
 return "NO";
 }
 }
 return "YES";
}
answered Dec 12, 2018 at 13:28
1
  • yes it works perfectly for all cases , can you tell me the things wrong with code logic ? @SamzSakerz Commented Dec 12, 2018 at 13:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.