Number of Steps to Reduce a Number in Binary Representation to One (LeetCode 1404): Solution and Explanation
Given a binary string s, repeatedly halve the number when it is even and add 1 when it is odd, and count the steps until it becomes 1. For s = "1101" (13) the answer is 6: 13, 14, 7, 8, 4, 2, 1.
Intuition
The string can be 500 bits long, far beyond 64-bit integers, so the simulation has to work on the bits directly. Luckily both operations have a simple effect on the binary form.
Dividing an even number by 2 drops its last bit, which must be 0. Adding 1 to an odd number turns its last bit from 1 to 0 and sends a carry to the bit above. So every bit except the leading one is eventually removed by exactly one division, and sometimes it needs one addition first.
Which bits need the addition? Scan from the right while keeping the carry that previous additions pushed up. The current last bit is s[i] + carry. If that is 1 the number is odd: add 1 (2 steps with the division) and the carry becomes 1. If it is 0 or 2 (which is 0 with the carry still moving), the number is even and one division is enough.
Once the carry becomes 1 it never drops back to 0, because a 0 bit plus carry 1 is odd and produces a new carry, and a 1 bit plus carry 1 passes the carry along. When the scan reaches the leading 1, a pending carry makes it 10, which needs one final division.
Approach: Right-to-left scan with a carry
- Set
steps = 0andcarry = 0. - For
ifrom the last index down to 1, computebit = s[i] + carry. - If
bit == 1, the number is odd: add 2 tosteps(one +1 and one ÷2) and setcarry = 1. - Otherwise the number is even: add 1 to
steps. - Return
steps + carry, since a carry at the top turns the leading 1 into 10.
Step-by-step example
s = "1101"
- i = 3: s[3] = 1, carry 0, so bit = 1. Odd: +1 and ÷2, steps = 2, carry = 1.
- i = 2: s[2] = 0 plus carry 1 gives 1. Odd again: steps = 4, carry stays 1.
- i = 1: s[1] = 1 plus carry 1 gives 2, so the bit is 0 and the carry continues. Even: steps = 5.
- Only the leading 1 is left and carry = 1, so the number is 10 and needs one more ÷2.
- Return 5 + 1 = 6.
Code: Python, Java, C++
1class Solution:
2 def numSteps(self, s: str) -> int:
3 steps = 0
4 carry = 0
5 for i in range(len(s) - 1, 0, -1):
6 bit = int(s[i]) + carry
7 if bit == 1:
8 steps += 2
9 carry = 1
10 else:
11 steps += 1
12 return steps + carry1class Solution {
2 public int numSteps(String s) {
3 int steps = 0;
4 int carry = 0;
5 for (int i = s.length() - 1; i > 0; i--) {
6 int bit = (s.charAt(i) - '0') + carry;
7 if (bit == 1) {
8 steps += 2;
9 carry = 1;
10 } else {
11 steps += 1;
12 }
13 }
14 return steps + carry;
15 }
16}1class Solution {
2public:
3 int numSteps(string s) {
4 int steps = 0;
5 int carry = 0;
6 for (int i = s.size() - 1; i > 0; i--) {
7 int bit = (s[i] - '0') + carry;
8 if (bit == 1) {
9 steps += 2;
10 carry = 1;
11 } else {
12 steps += 1;
13 }
14 }
15 return steps + carry;
16 }
17};Time and space complexity
O(n)
Each character of the string is read once.
O(1)
Only the step counter and the carry are stored.
Other approaches
Direct simulation on a bit array (time O(n^2), space O(n))
Keep the bits in a list, pop the last bit for a division, and propagate a real +1 through the list for odd numbers. It mirrors the statement but each addition can walk the whole list.
Big integer simulation (time O(n^2), space O(n))
In languages with arbitrary precision integers (Python, Java BigInteger), parse s and simulate the rules. Simple, but each operation costs O(n).
Common mistakes
- Parsing the string into a long or int. Strings longer than 63 bits overflow.
- Processing the leading bit inside the loop. It must stay as the final 1, so the loop stops at index 1.
- Forgetting the final carry. For "11" the loop gives 2 steps and the carry adds the third.
- Treating bit = 2 as odd. 1 + carry 1 means the bit is 0 with a carry, which is even.
Frequently asked questions
Why is every non-leading bit removed by exactly one division?
Additions never make the number longer except through the carry into the leading bit, and each division removes one bit from the right. The number reaches 1 once only the leading bit remains.
Why does the carry never return to 0?
With carry 1, a 0 bit becomes 1 (odd), and adding 1 produces a new carry. A 1 bit becomes 10, which keeps the carry. Either way it stays 1.
Can the answer be computed from the string without a loop over each bit?
Yes. If s is a power of two the answer is n - 1. Otherwise it is n + 1 plus the number of zeros that sit between the leading 1 and the lowest 1. The scan with a carry derives the same count step by step.