Reduce Piles with Repeated Half Removals

Quick Overview

Reduce Piles with Repeated Half Removals. For exactly k operations choose any pile and remove floor(pileSize/2) stones from it. A pile may be chosen repeatedly. Return the smallest possible sum of remaining stones.

Reduce Piles with Repeated Half Removals

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Perform exactly `k` operations on the stone piles. In each operation, choose any pile and remove `floor(pileSize/2)` stones from its current contents. The same pile may be selected repeatedly. Return the minimum possible total number of stones left after all required operations. ```cpp class Solution { public: int minStoneSum(vector<int>& piles, int k); }; ``` ### Examples **Example 1:** ```text piles = [8,3], k = 2 Output: 5 Explanation: Reducing 8 to 4 and then to 2 leaves 2+3 stones. ``` **Example 2:** ```text piles = [1,1], k = 3 Output: 2 Explanation: Every required operation removes zero stones. ``` ### Constraints - `1 <= piles.length <= 10^5`. - `1 <= piles[i] <= 10^4`; `1 <= k <= 10^5`. ### Clarifying Questions Remaining pile size is ceil(oldSize/2). Exactly k operations are made even when further removals are zero. ```hint Inspect the structure How do the successive removal amounts from one pile change? ``` ### Reasoning checkpoints - Justify a globally minimum remaining total across all choices of repeated pile operations. - Respect rounding and the exact operation count, including piles from which no more stones can be removed. ### Optional extensions How can the upper bound on pile sizes help reduce the cost of selecting each operation?

Overview: Reduce Piles with Repeated Half Removals. For exactly k operations choose any pile and remove floor(pileSize/2) stones from it. A pile may be chosen repeatedly. Return the smallest possible sum of remaining stones.

Salesforce logo
Salesforce
May 27, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Perform exactly k operations on the stone piles. In each operation, choose any pile and remove floor(pileSize/2) stones from its current contents. The same pile may be selected repeatedly. Return the minimum possible total number of stones left after all required operations.

class Solution {
public:
    int minStoneSum(vector<int>& piles, int k);
};

Examples

Example 1:

piles = [8,3], k = 2
Output: 5
Explanation: Reducing 8 to 4 and then to 2 leaves 2+3 stones.

Example 2:

piles = [1,1], k = 3
Output: 2
Explanation: Every required operation removes zero stones.

Constraints

  • 1 <= piles.length <= 10^5 .
  • 1 <= piles[i] <= 10^4 ; 1 <= k <= 10^5 .

Clarifying Questions Guidance

Remaining pile size is ceil(oldSize/2). Exactly k operations are made even when further removals are zero.

Reasoning checkpoints

  • Justify a globally minimum remaining total across all choices of repeated pile operations.
  • Respect rounding and the exact operation count, including piles from which no more stones can be removed.

Optional extensions

How can the upper bound on pile sizes help reduce the cost of selecting each operation?