Minimum Operations to Halve Array Sum - Problem

You are given an array nums of positive integers. In one operation, you can choose any number from nums and reduce it to exactly half the number. (Note that you may choose this reduced number in future operations.)

Return the minimum number of operations to reduce the sum of nums by at least half.

Input & Output

Example 1 — Basic Case
$ Input: nums = [5,19,8,1]
Output: 3
💡 Note: Original sum = 33, target reduction ≥ 16.5. Operations: halve 19→9.5 (reduction=9.5), halve 9.5→4.75 (total=14.25), halve 8→4 (total=18.25≥16.5). Answer: 3 operations.
Example 2 — Small Array
$ Input: nums = [3,8,20]
Output: 3
💡 Note: Original sum = 31, target reduction ≥ 15.5. Operations: halve 20→10 (reduction=10), halve 10→5 (total=15), halve 8→4 (total=19≥15.5). Answer: 3 operations.
Example 3 — Edge Case
$ Input: nums = [1,1]
Output: 2
💡 Note: Original sum = 2, target reduction ≥ 1. Operations: halve 1→0.5 (reduction=0.5), halve 1→0.5 (total=1≥1). Answer: 2 operations.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 107

Visualization

Tap to expand
Problem: Minimum Operations to Halve Array Sum51981Original sum = 33, Target reduction ≥ 16.5Always halve the largest element!Op 1: 19→9.5reduction: 9.5Op 2: 9.5→4.75total: 14.25Op 3: 8→4total: 18.25Output: 3 operations
Understanding the Visualization
1
Input
Array [5,19,8,1] with sum=33, need reduction≥16.5
2
Process
Greedily halve largest elements: 19→9.5→4.75, then 8→4
3
Output
3 operations needed to achieve target reduction
Key Takeaway
🎯 Key Insight: Always halve the largest element for maximum reduction impact per operation
Asked in
Amazon 35 Google 28 Microsoft 22 Meta 15
32.1K Views
Medium Frequency
~25 min Avg. Time
856 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