Buy the Most Bars Using Counting Sort

Quick Overview

Buy the Most Bars Using Counting Sort. Each price in costs belongs to one ice-cream bar. With coins coins, buy as many bars as possible in any order and return the greatest count.

Buy the Most Bars Using Counting Sort

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Each price in `costs` belongs to one ice-cream bar. With `coins` coins, buy as many bars as possible in any order and return the greatest count. Use counting sort, as required by the source, to process prices from least to greatest. ```cpp class Solution { public: int maxIceCream(vector<int>& costs, int coins); }; ``` ### Examples **Example 1:** ```text costs = [2,2,5,1], coins = 5 Output: 3 Explanation: Buy prices 1, 2, and 2. ``` **Example 2:** ```text costs = [4,6,8], coins = 3 Output: 0 Explanation: Even the cheapest bar exceeds the budget. ``` ### Constraints - `n == costs.length`, with `1 <= n <= 10^5`. - `1 <= costs[i] <= 10^5`. - `1 <= coins <= 10^8`. - The solution must use counting sort. ### Clarifying Questions A bar is indivisible and may be purchased at most once. Equal-priced bars are distinct available items. ```hint Inspect the structure Do equal-priced bars remain separate purchases, and can a single bar be bought more than once? ``` ### Reasoning checkpoints - Retain the explicit counting-sort requirement. - Prove that the selected bars are affordable and no larger count can be purchased. ### Optional extensions How does the algorithm’s cost depend on the maximum price as well as the number of bars?

Overview: Buy the Most Bars Using Counting Sort. Each price in costs belongs to one ice-cream bar. With coins coins, buy as many bars as possible in any order and return the greatest count.

Amazon logo
Amazon
May 31, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Each price in costs belongs to one ice-cream bar. With coins coins, buy as many bars as possible in any order and return the greatest count. Use counting sort, as required by the source, to process prices from least to greatest.

class Solution {
public:
    int maxIceCream(vector<int>& costs, int coins);
};

Examples

Example 1:

costs = [2,2,5,1], coins = 5
Output: 3
Explanation: Buy prices 1, 2, and 2.

Example 2:

costs = [4,6,8], coins = 3
Output: 0
Explanation: Even the cheapest bar exceeds the budget.

Constraints

  • n == costs.length , with 1 <= n <= 10^5 .
  • 1 <= costs[i] <= 10^5 .
  • 1 <= coins <= 10^8 .
  • The solution must use counting sort.

Clarifying Questions Guidance

A bar is indivisible and may be purchased at most once. Equal-priced bars are distinct available items.

Reasoning checkpoints

  • Retain the explicit counting-sort requirement.
  • Prove that the selected bars are affordable and no larger count can be purchased.

Optional extensions

How does the algorithm’s cost depend on the maximum price as well as the number of bars?