forked from NKaty/Algorithms-and-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber-of-steps-to-reduce-a-number-to-zero.js
More file actions
47 lines (38 loc) · 1.35 KB
/
number-of-steps-to-reduce-a-number-to-zero.js
File metadata and controls
47 lines (38 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Given a non-negative integer num, return the number of steps
// to reduce it to zero. If the current number is even,
// you have to divide it by 2, otherwise, you have to subtract 1 from it.
// Constraints:
// 0 <= num <= 10 ^ 6
const numberOfSteps = (num) => {
let count = 0;
while (num) {
// If the number is odd, subtract 1 from it
if (num & 1) num -= 1;
// If the number is even, divide it by 2
// The right shift by one is equivalent to dividing by 2
else num >>= 1;
count++;
}
return count;
};
console.log(numberOfSteps(123)); // 12
console.log(numberOfSteps(1000000)); // 26
// Reduces the number of iterations
const numberOfStepsImproved = (num) => {
if (!num) return num;
let count = 0;
while (num) {
// We reduce the number of iterations by combine two operation together
// If the number is odd, we need to subtract 1: + (num & 1) --> + 0 or 1
// Now the number is even and we need to perform the right shift: + 1
count += (num & 1) + 1;
// And now we go to the next bit
num >>= 1;
}
// The last bit will always be 1, so the last right shift we perform
// is unnecessary. We can just subtract the number by 1 to reduce it to zero
// To fix our count we subtract 1
return count - 1;
};
console.log(numberOfStepsImproved(123)); // 12
console.log(numberOfStepsImproved(1000000)); // 26