Minimum and Maximum Candy Cost Problem
Ram is in Ninjaland, visiting a unique candy store offering 'N' candies each with different costs. The store has a special offer: for every candy you purchase, you can take up to 'K' additional candies for free. Ram needs to determine both the minimum and maximum amounts of money he must spend to acquire all the candies.
Example:
Input:
N = 5, K = 2
candy_costs = [9, 8, 2, 6, 4]
Output:
Minimum = 6, Maximum = 17
Explanation:
Minimum Cost: Ram buys the candy costing 2, taking candies costing 9 and 8 for free, then buys the candy costing 4 and takes the candy costing 6 for free. Minimum cost = 2 + 4 = 6.
Maximum Cost: Ram buys the candy costing 9, taking candies costing 2 and 6 for free, then buys the candy costing 8 and takes the candy costing 4 for free. Maximum cost = 9 + 8 = 17.
Input:
The first line contains an integer 'T' denoting the number of test cases. Each of the following 'T' test cases consist of:
• The first line contains two integers 'N' and 'K', indicating the number of candies and maximum free candies per purchase respectively.
• The next line contains 'N' integers, each representing the cost of a candy.
Output:
For each test case, output two space-separated integers denoting the minimum and maximum amounts of money needed to purchase all candies.
Constraints:
- 1 <= T <= 5
- 1 <= N <= 105
- 0 <= K < N
- 1 <= candy_cost <= 109
Time limit for execution: 1 second.
Note:
The function implementation should only perform the required computation. Printing is handled by the system.
Find minimum and maximum cost to purchase all candies with special offer of free candies.
Iterate through the candy costs array and sort it in ascending order.
For minimum cost, start from the lowest co...read more
Top Google Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Google Software Developer
Reviews
Interviews
Salaries
Users/Month