25. Minimum Steps to Halve Sum
β GFG solution to the Minimum Steps to Halve Sum problem: find minimum operations to reduce array sum to half or less by repeatedly halving the largest element using greedy heap approach. π
π§© Problem Description
π Examples
Example 1
Input: arr[] = [8, 6, 2]
Output: 3
Explanation: Initial sum = (8 + 6 + 2) = 16, half = 8
Halve 8 β arr[] = [4, 6, 2], sum = 12 (still 12 > 8)
Halve 6 β arr[] = [4, 3, 2], sum = 9 (still 9 > 8)
Halve 4 β arr[] = [2, 3, 2], sum = 7 (now 7 < 8)
Hence, 3 operations are needed.Example 2
Input: arr[] = [9, 1, 2]
Output: 2
Explanation: Initial sum = 12, half = 6
Halve 9 β arr[] = [4.5, 1, 2], sum = 7.5 (still > 6)
Halve 4.5 β arr[] = [2.25, 1, 2], sum = 5.25 β€ 6
Hence, 2 operations are needed.π Constraints
β
My Approach
Greedy Heap Strategy
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated