Imagine that you are a ticket seller at a cinema and each ticket costs 25 us dollar, there are whole bunch of people lining up in line waiting to buy ticket for the hottest movie in town, each of them either has a 25, 50 or 100 dollar bill and you have no change at the beginning which means you will need to receive at least a 25 dollar bill from an audience before you can use it to make a change if someone pays you a 50 dollar bill, remember the ticket costs 25 US dollar each.

We will write a simple Javascript program which can solve the above situation. Bear in mind that the program will take in an array of US dollar bills represent the persons that line in line to buy the ticket, the first element of the array might not be 25 which means if the first person who lines in line has a hundred dollar bill then he or she will need to wait at the side for the ticket seller to make the change, since each ticket costs 25 USD then the seller will need to receive either three 25 dollar bills or a 50 dollar bill plus one 25 dollar bill from the cinema audiences before he or she can make a change to the hundred dollar bill’s holder. The program will also need to return “NO” for unable to make a change or “YES” for able to make all the changes after it has looped through all the bills in the array.

In order to make a change we will need to keep in mind that it is not about the amount of money the ticket seller has but instead is the number of 25 dollar bill or the 50 dollar bill that seller has. For example the seller certainly cannot make a change to the hundred dollar bill holder even he or she has 400 dollars that consist of 100 dollar bill each and the seller also unable to make a change to the 50 dollar bill holder even he or she has 2 pieces of 50 dollar bills because in order to make a change to the 50 dollar bill holder we will need one piece of 25 dollar bill because each ticket costs 25 US dollar.

Here are the steps we need in order to solve the above question.

1) If the ticket buyer pays us 25 dollar bill we will push it into the 25 dollar bill’s array.

2) If the ticket buyer pays us with a 50 dollar bill we will push the 50 dollar bill into the 50 dollar bill’s array and remove a 25 dollar bill from the 25 dollar bill’s array.

3) If the buyer pays a hundred dollar bill then we can either remove one 25 dollar bill from the 25 dollar bill’s array and a 50 dollar bill’s from a 50 dollar bill’s array or remove three 25 dollar bills from the 25 dollar bill’s array.

4) If we cannot make the change at the moment due to lack of 25 or 50 dollar bill then we will push the 100 or 50 dollar bill holder to a waiting list array.

5) If we still cannot make the change after looping through the waiting list array then we will return “NO” or else we will return “YES”. In the waiting list loop we will return “NO” and thus stop the program once we cannot make a change to a 50 or 100 dollar bill holder.

Well, so much for the theory below is the entire ticket changes code, do enjoy!

function tickets(peopleInLine) { let holdlist = []; let twentyfive = []; let fifty = []; for(let i = 0; i < peopleInLine.length; i++) { if(peopleInLine[i] == 25) { twentyfive.push(peopleInLine[i]); } else if(peopleInLine[i] == 50) { if(twentyfive.length > 0) { twentyfive.pop(); fifty.push(peopleInLine[i]); } else { holdlist.push(peopleInLine[i]); } } else if(peopleInLine[i] == 100) { if(fifty.length > 0 && twentyfive.length > 0) { fifty.pop(); twentyfive.pop(); } else if(twentyfive.length >= 3) { twentyfive.pop(); twentyfive.pop(); twentyfive.pop(); } else { holdlist.push(peopleInLine[i]); } } } // the waiting list loop for(let j = 0; j < holdlist.length; j++) { if(holdlist[j] == 50) { if(twentyfive.length > 0) { twentyfive.pop(); fifty.push(holdlist[j]); } else { return "NO"; } } else if(holdlist[j] == 100) { if(fifty.length > 0 && twentyfive.length > 0) { fifty.pop(); twentyfive.pop(); } else if(twentyfive.length >= 3) { twentyfive.pop(); twentyfive.pop(); twentyfive.pop(); } else { return "NO"; } } } return "YES"; }

If we enter below array into the function…

console.log(tickets([25,25,25,100,25,25,50,100,25,25,50,100,25,25,25,100,100,50]))

We will get the answer “NO”.

The solution above is not perfect and you certainly has a better one therefore do leave your solution below the comment box if you would like to.

## Leave a Reply

Be the First to Comment!