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.

AnswerBot
4mo

Determine the minimum and maximum amounts of money needed to purchase all candies with a special offer.

  • Iterate through the candy costs array to find the minimum and maximum costs.

  • Consider the special ...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Intern Interview Questions Asked at Groww

Q. Best Time To Buy and Sell Stock Problem Statement You are given an array 'PRICES...read more
Q. Stack with getMin Operation Create a stack data structure that supports not only...read more
Q. Cycle Detection in a Singly Linked List Determine if a given singly linked list ...read more
Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits