Problem
Minimum Operations to Zero
Learn this problemProblem statement
You are given an integer n. In one operation, you may add or subtract any power of two from n, such as 2^0, 2^1, 2^2, and so on.
Return the minimum number of operations required to reduce n to 0.
Function
getMinOperations(n: int) → intExamples
Example 1
n = 21return = 3One optimal sequence is 21 -> 20 by subtracting 1, 20 -> 16 by subtracting 4, and 16 -> 0 by subtracting 16.
Constraints
1 <= n <= 10^9
More Salesforce problems
- Minimize Total Input Cost (for LTMS)Seen Jun 2026
- Count Prime StringsONSITE INTERVIEW · Seen Jun 2026
- Final Pod Counts After LogsOA · Seen May 2026
- ATM Queue Exit OrderPHONE SCREEN · Seen May 2026
- Good Ways to Split an ArrayPHONE SCREEN · Seen May 2026
- Generate Seen Binary StringsOA · Seen May 2026
- Update Pod Counts From LogsOA · Seen May 2026
- Minimum Removals to Balance ArrayOA · Seen May 2026