You are given an integer n, where 0 < n < 2^60.
In one operation, you may add or subtract any power of two from n. That is, you may choose an integer k >= 0 and replace n with either n + 2^k or n - 2^k.
Return the minimum number of operations needed to reduce n to 0.
Examples
01 · Example 1
n = 7 return = 2
Add 1 to get 8, then subtract 8 to reach 0.
02 · Example 2
n = 10 return = 2
Subtract 8, then subtract 2.
Constraints
0 < n < 2^60
More Salesforce problems
- Count Prime StringsONSITE INTERVIEW · Seen Jun 2026
- Key Teams in TreeOA · Seen Mar 2026
- Update Logs by Symmetric XOROA · Seen Mar 2026
- Count Palindromic Concatenation PairsOA · Seen Mar 2026
- Collect Opportunity Data in a TreeOA · Seen Feb 2026
- Replace '?' to Avoid Adjacent DuplicatesOA · Seen Feb 2026
- Strings With No k Consecutive Identical CharactersOA · Seen Feb 2026
- Longest Subsequence which is a SubstringOA · Seen Dec 2025
public int minimumEnergyReductionOperations(long n) {
// write your code here
}n7
expected2
sign in to submit