Problem

System Energy Reduction

SalesforceFULLTIMEOA
See Salesforce hiring insights

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
drafts saved locally
public int minimumEnergyReductionOperations(long n) {
  // write your code here
}
n7
expected2
sign in to submit