Loading...
Loading...
Your class runs a lemonade stall at the college fest. One glass costs ₹5. Customers come one at a time, and each one pays with a single ₹5, ₹10 or ₹20 note. The stall starts with no money at all.
Each customer must get exact change at once, using only the notes the stall has received so far. You choose which notes to give back. Print YES if every customer gets the right change. Otherwise print NO and the position of the first customer who cannot get change. Customers are numbered from 1.
YES, or NO k where k is the position of the first customer who cannot get change.Example 1
Input
5 5 5 5 10 20
Output
YES
The 10 gets one 5 back. The 20 then gets the 10 and a 5 back. Everyone gets change.
Example 2
Input
5 5 5 10 10 20
Output
NO 5
After the two 10s, the stall holds only two ₹10 notes. Customer 5 needs ₹15 back and that is not possible.
Try the problem first. Open one hint at a time.
You only need to know how many ₹5 notes and how many ₹10 notes the stall holds. A ₹20 note is never useful as change.
A ₹10 payment always needs one ₹5 back.
A ₹20 payment can get a 10 and a 5, or three 5s. Which notes are more useful to keep for later?
A ₹5 note can make change for anyone, a ₹10 note cannot. So give a 10 and a 5 when you can, and three 5s only otherwise.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor