# 1342. Number of Steps to Reduce a Number to Zero

`Input: num = 14Output: 6Explanation: Step 1) 14 is even; divide by 2 and obtain 7. Step 2) 7 is odd; subtract 1 and obtain 6.Step 3) 6 is even; divide by 2 and obtain 3. Step 4) 3 is odd; subtract 1 and obtain 2. Step 5) 2 is even; divide by 2 and obtain 1. Step 6) 1 is odd; subtract 1 and obtain 0.`
`Input: num = 8Output: 4Explanation: Step 1) 8 is even; divide by 2 and obtain 4. Step 2) 4 is even; divide by 2 and obtain 2. Step 3) 2 is even; divide by 2 and obtain 1. Step 4) 1 is odd; subtract 1 and obtain 0.`
`Input: num = 123Output: 12`
• `0 <= num <= 10^6`
`class Solution:    def numberOfSteps(self, num: int) -> int:        cnt=0        last=0        while(num):            last=[0,1][num&1]            if(last):                num-=1            else:                num//=2            cnt+=1            return(cnt)`
`class Solution:    def numberOfSteps(self, num: int) -> int:        cnt=0        last=0        while(num):            last=[0,1][num&1]            if(last):                num=num^1            else:                num>>=1            cnt+=1            return(cnt)`

--

--

--

## More from Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

Love podcasts or audiobooks? Learn on the go with our new app.

## Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.