← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
You are given a positive integer n. In one operation, you may either add or subtract any power of two. Return the minimum number of operations required to make n equal to 0. To preserve the original n < 2^60 bound exactly in this practice environment, n is provided to your function as a base-10 string.
Example 1:
Input: n = "16" Output: 5 Explanation: We can do: n - 1 = 15 n - 1 = 14 n - 1 = 13 n + 1 = 14 n - 1 = 0 The total number of operations is 5.
Example 2:
Input: n = "821" Output: 11 Explanation: We can do: n - 1 = 820 n - 10 = 810 n - 100 = 710 n - 1000 = 10 n + 1 = 11 n + 1 = 10 n + 100 = 110 n + 100 = 210 n + 1000 = 1210 n - 1000 = 210 n + 1 = 211 n - 1 = 210 n - 10 = 200 The total number of operations is 11.
1 <= n.length <= 5 * 10^4n consists of only digits.1 <= n < 2^60