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.

Be the first one to answer
Add answer anonymously...
Groww Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter