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

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

Help your peers!
Select
Add answer anonymously...

Google Software Developer interview questions & answers

A Software Developer was asked 1mo agoQ. What are your goals?
A Software Developer was asked 2mo agoQ. What is Dijkstra's algorithm and how does it work?
A Software Developer was asked 2mo agoQ. What is the explanation of the Least Recently Used (LRU) cache algorithm?

Popular interview questions of Software Developer

A Software Developer was asked 1mo agoQ1. What are your goals?
A Software Developer was asked 2mo agoQ2. What is Dijkstra's algorithm and how does it work?
A Software Developer was asked 2mo agoQ3. What is the explanation of the Least Recently Used (LRU) cache algorithm?

Top HR questions asked in Google Software Developer

A Software Developer was asked 2mo agoQ1. Why should we choose you?
A Software Developer was asked 3mo agoQ2. How does the role fit your career plans?
A Software Developer was asked 8mo agoQ3. Explain your project in detail.
Google Software Developer 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