Loading...
Loading...
Loading...
A shopping cart must fit a budget. While the total of the cart is above the budget, remove the most expensive item (the first one when several cost the same). Print the price of each removed item, then the final total and the number of items left.
Removed <price> for each removed item, in order, then Total: <total> and Items left: <count>.Example
Input
5 300 1200 450 90 800 1500
Output
Removed 1200 Removed 800 Total: 840 Items left: 3
The total 2840 is above 1500. Without the 1200 it is 1640, still above. Without the 800 it is 840.
Try the problem first. Open one hint at a time.
Keep the running total, and subtract each price you remove.
To find the first most expensive item, loop with an index and update the best index only when an item is strictly bigger.
list.remove(index) with an int index returns the removed price.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor