Problem · Math
Minimum Operations To Reduce To Zero
Learn this problemProblem statement
Given a positive integer n, you can either add or subtract 2^i in a single operation where i >= 0. Determine the minimum number of operations required to reduce n to 0.
Find the minimum number of operations required to convert n to 0.
Constraints
1 <= n < 2^40
Function
getMinOperations(n: long) → intExamples
Example 1
n = 6return = 2n can be reduced to 0 using two operations:
- Choose
i = 1and subtract2^1from6, so6 - 2 = 4. - Choose
i = 2and subtract2^2from4, so4 - 4 = 0.
The answer is 2.
Example 2
n = 21return = 3One sequence of operations is:
- Choose
i = 0, subtract2^0 = 1from21, converting it to20. - Choose
i = 2, subtract2^2 = 4from20, converting it to16. - Choose
i = 4, subtract2^4 = 16from16, converting it to0.
More Uber problems
- Last Truck to Leave the LaneOA · Seen Jul 2026
- Chain of CommandOA · Seen Jul 2026
- Jump Game with Prime-3 StepsOA · Seen Jun 2026
- Total Palindrome Substring CostOA · Seen Jun 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · Seen May 2026
- Tournament Rounds by RankPHONE SCREEN · Seen May 2026
- Farthest Seat AssignmentONSITE INTERVIEW · Seen May 2026
- Convex Function MinimizationPHONE SCREEN · Seen May 2026