Problem

System Energy Reduction

Learn this problem
Salesforce logoSalesforceFULLTIMEOA
See Salesforce hiring insights

Problem statement

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.

Function

minimumEnergyReductionOperations(n: long) → int

Examples

Example 1

n = 7return = 2

Add 1 to get 8, then subtract 8 to reach 0.

Example 2

n = 10return = 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
checking account