Advertisement

Number of Steps to Reduce a Number in Binary Representation to One - LeetCode 1404 Solution

Number of Steps to Reduce a Number in Binary Representation to One - Complete Solution Guide

Number of Steps to Reduce a Number in Binary Representation to One is LeetCode problem 1404, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 : If the current number is even, you have to divide it by 2 . If the current number is odd, you have to add 1 to it. 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, divid

Detailed Explanation

The problem requires us to find the number of steps to reduce a number represented in binary format (given as a string) to 1. The steps involve two operations: if the number is even, divide it by 2; if the number is odd, add 1 to it. We need to return the total number of steps taken to reach 1.

Solution Approach

The provided solution simulates the process of reducing the binary number to 1 step by step. It iterates through the binary string from right to left, keeping track of a `carry`. If the current bit plus carry is odd (equal to 1), it means adding 1 to the current number results in two steps: one for the addition and one implicit division by 2 (right shift). If the current bit plus carry is even (equal to 0 or 2), it means dividing the current number by 2, which needs only one step. The `carry` variable maintains the carry-over from previous additions. Finally, if there is a carry after processing all bits except the leftmost bit, one additional division step is required.

Step-by-Step Algorithm

  1. Step 1: Initialize `steps` and `carry` to 0.
  2. Step 2: Iterate through the binary string `s` from the second rightmost bit (index `len(s) - 1`) to the second bit (index 1).
  3. Step 3: For each bit `s[i]`, check if the sum of the bit value (as an integer) and the `carry` is equal to 1. `if int(s[i]) + carry == 1:`
  4. Step 4: If the sum is 1, increment `steps` by 2 (one for adding 1, the other for dividing by 2) and set `carry` to 1.
  5. Step 5: If the sum is not 1 (meaning it's 0 or 2), increment `steps` by 1 (for dividing by 2). If the sum is 2, the `carry` is set to 1, else it remains 0.
  6. Step 6: After the loop, add the final `carry` to `steps`. This accounts for the final division step if the carry is 1.
  7. Step 7: Return the final `steps` count.

Key Insights

  • Insight 1: We don't need to convert the binary string to an integer. We can simulate the operations directly on the binary string by observing the last bit.
  • Insight 2: Dividing by 2 in binary is equivalent to right-shifting (removing the last bit).
  • Insight 3: Adding 1 to a binary number can result in carry-over operations, similar to addition in decimal.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: String, Bit Manipulation, Simulation.

Companies

Asked at: Geico, Grab.