Solution: Number of Steps to Reduce a Number to Zero
Tags: algorithms, javascript, java, leetcode
Published: 2021-02-12 17:45:45

*This is part of a series of Leetcode solution explanations ([index](https://dev.to/seanpgallivan/leetcode-solutions-index-57fl)). If you liked this solution or found it useful,* ***please like*** *this post and/or* ***upvote*** *[my solution post on Leetcode's forums](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/discuss/1061946).*

---

####[Leetcode Problem #1342 (*Easy*): Number of Steps to Reduce a Number to Zero](https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/)####

---

***Description:***

*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.*

---

***Examples:***

|Example 1:||
|---:|---|
*Input:*|nums = [1,2,3]
*Output:*|[1,3,2]

Example 1:||
|---:|---|
*Input:*|num = 14
*Output:*|6
*Explanation:*|Step 1) 14 is even; divide by 2 and obtain 7.<br>Step 2) 7 is odd; subtract 1 and obtain 6.<br>Step 3) 6 is even; divide by 2 and obtain 3. <br>Step 4) 3 is odd; subtract 1 and obtain 2.<br>Step 5) 2 is even; divide by 2 and obtain 1.<br>Step 6) 1 is odd; subtract 1 and obtain 0.

Example 2:||
|---:|---|
*Input:*|num = 8
*Output:*|4
*Explanation:*|Step 1) 8 is even; divide by 2 and obtain 4.<br>Step 2) 4 is even; divide by 2 and obtain 2.<br>Step 3) 2 is even; divide by 2 and obtain 1.<br>Step 4) 1 is odd; subtract 1 and obtain 0.

Example 3:||
|---:|---|
*Input:*|num = 123
Output:*|12

---

***Constraints:***
 - `0 <= num <= 10^6`

---

***Idea:***

For this problem, we just have to follow the directions: we can tell if **num** is odd by using **modulo 2**; **num % 2** is **1** if **num** is odd, otherwise it's **0**. Anytime **num** is odd, subtract **1** from **num**. Anytime **num** is even, divide **num** by **2**.

While we do this, we can just increment our counter (**ans**) and then **return** it once **num** reaches **0**.

---

***Javascript Code:***

The best result for the code below is **68ms / 37.1MB** (beats 99% / 100%).
```javascript
var numberOfSteps  = function(num) {
    let ans = 0
    for (; num; ans++)
        if (num % 2) num--
        else num /= 2
    return ans
};
```

---

***Python Code:***

The best result for the code below is **24ms / 14.1MB** (beats 96% / 89%).
```python
class Solution:
    def numberOfSteps (self, num: int) -> int:
        ans = 0
        while num > 0:
            if num % 2: num -= 1
            else: num /= 2
            ans += 1
        return ans
```

---

***Java Code:***

The best result for the code below is **0ms / 35.5MB** (beats 100% / 95%).
```java
class Solution {
    public int numberOfSteps (int num) {
        int ans = 0;
        for (; num > 0; ans++)
            if (num % 2 == 1) num--;
            else num /= 2;
        return ans;
    }
}
```

---

***C++ Code:***

The best result for the code below is **0ms / 5.8MB** (beats 100% / 85%).
```c++
class Solution {
public:
    int numberOfSteps (int num) {
        int ans = 0;
        for (; num > 0; ans++)
            if (num % 2 == 1) num--;
            else num /= 2;
        return ans;
    }
};
```