Number of Steps to Reduce a Number in Binary Representation to One in C++



Suppose we have a number s in binary form. We have to find the number of steps to reduce it to 1 under these rules −

  • If the current number is even, we have to divide it by 2.

  • If the current number is odd, you have to add 1 to it.

So, if the input is like "1101", then the output will be 6, as "1101" is 13. So, 13 is odd, add 1 and obtain 14. Then 14 is even, divide by 2 and obtain 7. After that 7 is odd, add 1 and obtain 8.

Then 8 is again, even so, divide by 2 and obtain 4. Again 4 is even, divide by 2 and obtain 2, finally 2 is even, divide by 2 and obtain 1.

To solve this, we will follow these steps −

  • Define a function addStrings(), this will take an array num1, an array num2,

  • Define an array ret

  • carry := 0, sum := 0

  • reverse num1 and num2

  • i := 0, j := 0

  • while (i < size of num1 or j < size of num2), do −

    • if i < size of num1 and j < size of num2, then −

      • sum := carry + (num1[i] + num2[j])

      • insert sum mod 2 at the end of ret

      • carry := sum / 2

      • (increase i by 1)

      • (increase j by 1)

    • otherwise when i < size of num1, then−

      • sum := carry + (num1[i])

      • insert sum mod 2 at the end of ret

      • carry := sum / 2

      • (increase i by 1)

    • Otherwise

      • sum := carry + (num2[j])

      • insert sum mod 2 at the end of ret

      • carry := sum / 2

      • (increase j by 1)

  • if carry is non-zero, then −

    • insert carry at the end of ret

  • i := size of ret

  • ans := blank string

  • for i >= 0, update (decrease i by 1), do −

    • ans := ans + (ret[i] + ASCII of '0')

  • return (if size of ans is same as 0, then "0", otherwise ans)

  • Define a function addBinary(), this will take an array a, an array b,

  • return addStrings(a, b)

  • Define an array makeVector and copy from v

    • Define an array ret

    • for initialize i := 0, when i < size of v, update (increase i by 1), do −

      • insert v[i] - ASCII of '0' at the end of ret

    • return ret

  • From the main method do the following,

  • ret := 0

  • Define an array x = makeVector from s

  • while size of x > 1, do −

    • (increase ret by 1)

    • if last element of x is same as 0, then −

      • delete last element from x

    • Otherwise

      • Define an array temp of size 1

      • temp[0] := 1

      • x := makeVector(addBinary(x, temp))

  • return ret

Example 

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h> using namespace std; class Solution { public:    string addStrings(vector<int> num1, vector<int> num2){       vector<int> ret;       int carry = 0;       int sum = 0;       reverse(num1.begin(), num1.end());       reverse(num2.begin(), num2.end());       int i = 0;       int j = 0;       while (i < num1.size() || j < num2.size()) {          if (i < num1.size() && j < num2.size()) {             sum = carry + (num1[i]) + (num2[j]);             ret.push_back(sum % 2);             carry = sum / 2;             i++;             j++;          }          else if (i < num1.size()) {             sum = carry + (num1[i]);             ret.push_back(sum % 2);             carry = sum / 2;             i++;          }          else {             sum = carry + (num2[j]);             ret.push_back(sum % 2);             carry = sum / 2;             j++;          }       }       if (carry)          ret.push_back(carry);       i = ret.size() - 1;       string ans = "";       for (; i >= 0; i--)          ans += (ret[i] + '0');       return ans.size() == 0 ? "0" : ans;    }    string addBinary(vector<int>& a, vector<int>& b){       return addStrings(a, b);    }    vector<int> makeVector(string v){       vector<int> ret;       for (int i = 0; i < v.size(); i++)          ret.push_back(v[i] - '0');       return ret;    }    int numSteps(string s){       int ret = 0;       vector<int> x = makeVector(s);       while (x.size() > 1) {          ret++;          if (x.back() == 0) {             x.pop_back();          }          else {             vector<int> temp(1);             temp[0] = 1;             x = makeVector(addBinary(x, temp));          }       }       return ret;    } }; main(){    Solution ob;    cout << (ob.numSteps("1101")); }

Input

"1101"

Output

6
Updated on: 2020-11-17T11:42:40+05:30

260 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements