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

In this guide, you will get 1404. Number of Steps to Reduce a Number in Binary Representation to One LeetCode Solution with the best time and space complexity. The solution to Number of Steps to Reduce a Number in Binary Representation to One problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Number of Steps to Reduce a Number in Binary Representation to One solution in C++
  4. Number of Steps to Reduce a Number in Binary Representation to One solution in Java
  5. Number of Steps to Reduce a Number in Binary Representation to One solution in Python
  6. Additional Resources
1404. Number of Steps to Reduce a Number in Binary Representation to One LeetCode Solution image

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

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, 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:

1 <= s.length <= 500
s consists of characters '0' or '1'
s[0] == '1'

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1404. Number of Steps to Reduce a Number in Binary Representation to One LeetCode Solution in C++

class Solution {
 public:
  int numSteps(string s) {
    int ans = 0;

    // All the trailing 0s can be popped by 1 step.
    while (s.back() == '0') {
      s.pop_back();
      ++ans;
    }

    if (s == "1")
      return ans;

    // `s` is now odd, so add 1 to `s` and cost 1 step.
    ++ans;

    // All the 1s will become 0s and can be popped by 1 step.
    // All the 0s will become 1s and can be popped by 2 steps (adding 1 then
    // dividing by 2).
    for (const char c : s)
      ans += c == '1' ? 1 : 2;

    return ans;
  }
};
/* code provided by PROGIEZ */

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

class Solution {
  public int numSteps(String s) {
    int ans = 0;
    StringBuilder sb = new StringBuilder(s);

    // All the trailing 0s can be popped by 1 step.
    while (sb.charAt(sb.length() - 1) == '0') {
      sb.deleteCharAt(sb.length() - 1);
      ++ans;
    }

    if (sb.toString().equals("1"))
      return ans;

    // `s` is now odd, so add 1 to `s` and cost 1 step.
    ++ans;

    // All the 1s will become 0s and can be popped by 1 step.
    // All the 0s will become 1s and can be popped by 2 steps (adding 1 then
    // dividing by 2).
    for (final char c : sb.toString().toCharArray())
      ans += c == '1' ? 1 : 2;

    return ans;
  }
}
// code provided by PROGIEZ

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

class Solution:
  def numSteps(self, s: str) -> int:
    ans = 0
    chars = list(s)

    # All the trailing 0s can be popped by 1 step.
    while chars[-1] == '0':
      chars.pop()
      ans += 1

    if ''.join(chars) == '1':
      return ans

    # `s` is now odd, so add 1 to `s` and cost 1 step.
    # All the 1s will become 0s and can be popped by 1 step.
    # All the 0s will become 1s and can be popped by 2 steps (adding 1 then
    # dividing by 2).
    return ans + 1 + sum(1 if c == '1' else 2 for c in chars)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.