Maximum Score From Removing Stones - Problem

You are playing a solitaire game with three piles of stones of sizes a, b, and c respectively.

Each turn you choose two different non-empty piles, take one stone from each, and add 1 point to your score. The game stops when there are fewer than two non-empty piles (meaning there are no more available moves).

Given three integers a, b, and c, return the maximum score you can get.

Input & Output

Example 1 — Balanced Case
$ Input: a = 2, b = 4, c = 6
Output: 6
💡 Note: We can take stones from different piles: (6,4), (5,3), (4,2), (3,2), (2,1), (1,1) → 6 moves total
Example 2 — Unbalanced Case
$ Input: a = 4, b = 4, c = 6
Output: 7
💡 Note: Total = 14, so max possible is 7. Two smaller piles sum to 8, so we can achieve 7 points
Example 3 — Large Pile Dominates
$ Input: a = 1, b = 8, c = 8
Output: 8
💡 Note: Limited by sum of two smaller piles: min(17/2, 1+8) = min(8, 9) = 8

Constraints

  • 1 ≤ a, b, c ≤ 105

Visualization

Tap to expand
Maximum Score From Removing Stones INPUT Three Piles of Stones Pile A a = 2 Pile B b = 4 Pile C c = 6 Input Values: a=2, b=4, c=6 Each turn: pick 2 piles, remove 1 from each, +1 point Total: 2+4+6 = 12 stones ALGORITHM STEPS 1 Sort Piles Order: a <= b <= c [2, 4, 6] 2 Check Condition If a+b <= c: score = a + b 3 Otherwise score = (a+b+c) / 2 4 Apply Formula 2+4 = 6 <= 6 (true) score = 2 + 4 = 6 Greedy Moves: (B,C): [2,3,5] +1 (B,C): [2,2,4] +1 (A,C): [1,2,3] +1 (B,C): [1,1,2] +1 (A,C): [0,1,1] +1 (B,C): [0,0,0] +1 FINAL RESULT Final State: All Piles Empty A: 0 B: 0 C: 0 Maximum Score 6 Output: 6 [OK] Optimal solution found All 12 stones used in 6 moves Key Insight: When the largest pile (c) is greater than or equal to the sum of the other two piles (a+b), we can only score a+b points (pairing each stone from a and b with c). Otherwise, we can pair all stones optimally for a score of (a+b+c)/2 since each move removes exactly 2 stones from the total. TutorialsPoint - Maximum Score From Removing Stones | Greedy Strategy
Asked in
Google 15 Amazon 12 Microsoft 8
28.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen