Chocolate Buying Spree

Easy
0/40
0 upvote

Problem statement

A kid has a budget of exactly ₹100 and wants to buy chocolates. A list of available chocolates is provided, with each chocolate having a specific price.


Your task is to write a program that calculates two separate metrics based on this scenario:


Maximum Quantity: The absolute maximum number of individual chocolates the kid can buy without exceeding the ₹100 budget. To achieve this, the kid should prioritize buying the cheapest chocolates first.

Maximum Variety: The maximum number of different kinds of chocolates the kid can buy. A "kind" of chocolate is defined by its price. To achieve this, the kid should buy one of each of the cheapest unique kinds of chocolate first.


Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer N, the number of chocolates available.

The second line contains N space-separated integers, representing the prices of the chocolates.


Output Format:
Your function should return the max_quantity and max_variety as a pair or a small array/list.


Note:
These two calculations are independent of each other. The strategy to maximize quantity is different from the strategy to maximize variety, and they should be computed separately.
Sample Input 1:
5
10 20 30 40 50


Sample Output 1:
4
4


Explanation for Sample 1:
Max Quantity: To buy the most chocolates, the kid buys the cheapest ones: 10 + 20 + 30 + 40 = 100. This uses the full budget and results in 4 chocolates.

Max Variety: The unique prices are 10, 20, 30, 40, 50. To buy the most kinds, the kid buys the cheapest kinds: 10 + 20 + 30 + 40 = 100. This results in 4 different kinds.


Sample Input 2:
5
10 10 10 80 90


Sample Output 2:
3
2


Explanation for Sample 2:
Max Quantity: The prices are [10, 10, 10, 80, 90]. The kid buys the three cheapest chocolates: 10 + 10 + 10 = 30. The budget left is 70, which is not enough for the next cheapest (80). The maximum quantity is 3.

Max Variety: The unique prices are {10, 80, 90}. The kid buys one of the cheapest kind (10), leaving ₹90. Then, they buy one of the next cheapest kind (80), leaving ₹10. This is not enough for the next kind (90). The maximum variety is 2.


Expected Time Complexity:
The expected time complexity is O(N log N).


Constraints:
0 <= N <= 1000
1 <= Price of each chocolate <= 100

Time limit: 1 sec
Approaches (1)
Time Complexity
Space Complexity
Code Solution
(100% EXP penalty)
Chocolate Buying Spree
Full screen
Console