The Indian Engineer

Problem 1404 Number of Steps to Reduce a Number in Binary Representation to One

Posted on 6 mins

String Bit-Manipulation

Problem Statement

Link - Problem 1404

Question

Given the binary representation of an integer as a string s, return the number of steps to reduce it to 1 under the following rules:

It is guaranteed that you can always reach one for all test cases.

Example 1:

Input: s = "1101"
Output: 6
Explanation: "1101" corressponds to number 13 in their decimal representation.
Step 1) 13 is odd, add 1 and obtain 14. 
Step 2) 14 is even, divide by 2 and obtain 7.
Step 3) 7 is odd, add 1 and obtain 8.
Step 4) 8 is even, divide by 2 and obtain 4.  
Step 5) 4 is even, divide by 2 and obtain 2. 
Step 6) 2 is even, divide by 2 and obtain 1.  

Example 2:

Input: s = "10"
Output: 1
Explanation: "10" corresponds to number 2 in their decimal representation.
Step 1) 2 is even, divide by 2 and obtain 1.  

Example 3:

Input: s = "1"
Output: 0

Constraints:

Solution

class Solution {
public:
    int numSteps(string s) {
        int ans = 0, i = s.size()-1;
        while(i>=0)
        {
            if(s[i]=='0')
            {
                ans++;i--;
                continue;
            }
            else if(i==0)
            {
                return ans;
            }
            else
            {

                while(i>=0 && s[i]=='1')
                {
                    ans++;i--;
                }
                if(i>=0)
                {
                    s[i]='1';
                }
                ans++;
            }
        }

        return ans;
    }
};

Complexity Analysis

| Algorithm         | Time Complexity | Space Complexity |
| ----------------- | --------------- | ---------------- |
| Carry propagation | O(n)            | O(1)             |

Explanation

Intial Thoughts

To approach this problem, consider the binary representation of a number as a sequence of operations. The goal is to reach ‘1’ in the fewest steps possible. Think of it like navigating through a maze, where each step is either a division by 2 (if the current number is even) or an addition of 1 (if the current number is odd). The key is to understand the impact of these operations on the binary representation of the number. Consider breaking down the problem into smaller parts, focusing on the rightmost bits of the binary representation first. Also, note that the problem guarantees that it’s always possible to reach ‘1’ for all test cases, so the focus should be on minimizing the number of steps required.

Intuitive Analysis

Intuitively, solving this problem involves understanding the pattern of operations required to reduce a binary number to ‘1’. One approach is to think of the process as a series of ‘carries’ in the binary representation. When a ‘1’ bit is encountered in the middle of the sequence, it triggers a chain reaction of ‘carries’ that need to be propagated to the left until a ‘0’ is found. This process can be seen as a sequence of additions and divisions that gradually reduce the number. Another useful intuition is to recognize that the number of steps required is closely related to the number of ‘1’ bits in the binary representation, especially those that are not at the very right. By understanding how these ‘1’ bits affect the sequence of operations, it’s possible to develop an efficient strategy for minimizing the number of steps.

1. Intuition

2. Implementation


Complexity Analysis

Time Complexity:

Space Complexity:


Footnote

This question is rated as Medium difficulty.

Hints

Read the string from right to left, if the string ends in ‘0’ then the number is even otherwise it is odd.

Simulate the steps described in the binary string.


Similar Questions:

Title URL Difficulty
Minimum Moves to Reach Target Score https://leetcode.com/problems/minimum-moves-to-reach-target-score Medium