Given a binary string *str*, the task is to print the numbers of steps required to convert it to one by the following operations:

- If
**‘S’**is odd add 1 to it. - If
**‘S’**is even divide it by 2.

**Examples:**

Input:str = “1001001”

Output:12

Input:str = “101110”

Output:8Number ‘101110’ is even, after dividing it by 2 we get an odd number ‘10111’ so we will add 1 to it. Then we’ll get ‘11000’ which is even and can be divide three times continuously in a row and get ’11’ which is odd, adding 1 to it will give us ‘100’ which is even and can be divided 2 times in a row. As, a result we get 1.

So 8 times the above two operations were required in this number.

Below is the step by step algorithm to solve this problem:

- Initialize the string S as a binary number.
- If the size of the binary is 1, then the required number of actions will be 0.
- If the last digit is 0, then its an even number so one operation is required to divide it by 2.
- After encountering 1, traverse till you get 0, with every digit one operation will take place.
- After encountering 0 after 1 while traversing, replace 0 by 1 and start from step 4 again.

Below is the implementation of above algorithm:

## C++

`// C++ program to count the steps ` `// required to convert a number to 1 ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long ` ` ` `// function to calculate the number of actions ` `int` `calculate_(string s) ` `{ ` ` ` `// if the size of binary is 1 ` ` ` `// then the number of actions will be zero ` ` ` `if` `(s.size() == 1) ` ` ` `return` `0; ` ` ` ` ` `// initializing the number of actions as 0 at first ` ` ` `int` `count_ = 0; ` ` ` `for` `(` `int` `i = s.length() - 1; i > 0;) { ` ` ` `// start traversing from the last digit ` ` ` `// if its 0 increment the count and decrement i ` ` ` `if` `(s[i] == ` `'0'` `) { ` ` ` `count_++; ` ` ` `i--; ` ` ` `} ` ` ` `// if s[i] == '1' ` ` ` `else` `{ ` ` ` `count_++; ` ` ` ` ` `// stop until you get 0 in the binary ` ` ` `while` `(s[i] == ` `'1'` `&& i > 0) { ` ` ` `count_++; ` ` ` `i--; ` ` ` `} ` ` ` `if` `(i == 0) ` ` ` `count_++; ` ` ` ` ` `// when encounter a 0 replace it with 1 ` ` ` `s[i] = ` `'1'` `; ` ` ` `} ` ` ` `} ` ` ` `return` `count_; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string s; ` ` ` `s = ` `"10000100000"` `; ` ` ` ` ` `cout << calculate_(s); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`//Java program to count the steps ` `//required to convert a number to 1 ` ` ` `public` `class` `ACX { ` ` ` ` ` `//function to calculate the number of actions ` ` ` `static` `int` `calculate_(String s) ` ` ` `{ ` ` ` `// if the size of binary is 1 ` ` ` `// then the number of actions will be zero ` ` ` `if` `(s.length() == ` `1` `) ` ` ` `return` `0` `; ` ` ` ` ` `// initializing the number of actions as 0 at first ` ` ` `int` `count_ = ` `0` `; ` ` ` `char` `[] s1=s.toCharArray(); ` ` ` `for` `(` `int` `i = s.length() - ` `1` `; i > ` `0` `😉 { ` ` ` `// start traversing from the last digit ` ` ` `// if its 0 increment the count and decrement i ` ` ` `if` `(s1[i] == ` `'0'` `) { ` ` ` `count_++; ` ` ` `i--; ` ` ` `} ` ` ` `// if s[i] == '1' ` ` ` `else` `{ ` ` ` `count_++; ` ` ` ` ` `// stop until you get 0 in the binary ` ` ` `while` `(s1[i] == ` `'1'` `&& i > ` `0` `) { ` ` ` `count_++; ` ` ` `i--; ` ` ` `} ` ` ` `if` `(i == ` `0` `) ` ` ` `count_++; ` ` ` ` ` `// when encounter a 0 replace it with 1 ` ` ` `s1[i] = ` `'1'` `; ` ` ` `} ` ` ` `} ` ` ` `return` `count_; ` ` ` `} ` ` ` ` ` `//Driver code ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` ` ` `String s; ` ` ` `s = ` `"10000100000"` `; ` ` ` ` ` `System.out.println(calculate_(s)); ` ` ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python3 program to count the steps ` `# required to convert a number to 1 ` ` ` `# Method to calculate the number of actions ` `def` `calculate_(s): ` ` ` ` ` `# if the size of binary is 1 ` ` ` `# then the number of actions will be zero ` ` ` `if` `len` `(s) ` `=` `=` `1` `: ` ` ` `return` `0` ` ` ` ` `# initializing the number of actions as 0 at first ` ` ` `count_ ` `=` `0` ` ` `i ` `=` `len` `(s) ` `-` `1` ` ` `while` `i > ` `0` `: ` ` ` ` ` `# start traversing from the last digit ` ` ` `# if its 0 increment the count and decrement i ` ` ` `if` `s[i] ` `=` `=` `'0'` `: ` ` ` `count_ ` `+` `=` `1` ` ` `i ` `-` `=` `1` ` ` ` ` `# if s[i] == '1' ` ` ` `else` `: ` ` ` `count_ ` `+` `=` `1` ` ` ` ` `# stop until you get 0 in the binary ` ` ` `while` `s[i] ` `=` `=` `'1'` `and` `i > ` `0` `: ` ` ` `count_ ` `+` `=` `1` ` ` `i ` `-` `=` `1` ` ` `if` `i ` `=` `=` `0` `: ` ` ` `count_ ` `+` `=` `1` ` ` `# when encounter a 0 replace it with 1 ` ` ` `s ` `=` `s[:i] ` `+` `"1"` `+` `s[i ` `+` `1` `:] ` ` ` `return` `count_ ` ` ` `# Driver code ` `s ` `=` `"10000100000"` `print` `(calculate_(s)) ` ` ` `# This code is contributed by ` `# Rajnis09 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the steps ` `//required to convert a number to 1 ` `using` `System; ` `class` `GFG ` `{ ` ` ` ` ` `// function to calculate the number of actions ` ` ` `static` `int` `calculate_(String s) ` ` ` `{ ` ` ` `// if the size of binary is 1 ` ` ` `// then the number of actions will be zero ` ` ` `if` `(s.Length == 1) ` ` ` `return` `0; ` ` ` ` ` `// initializing the number of actions as 0 at first ` ` ` `int` `count_ = 0; ` ` ` `char` `[] s1 = s.ToCharArray(); ` ` ` `for` `(` `int` `i = s.Length - 1; i > 0;) ` ` ` `{ ` ` ` `// start traversing from the last digit ` ` ` `// if its 0 increment the count and decrement i ` ` ` `if` `(s1[i] == ` `'0'` `) ` ` ` `{ ` ` ` `count_++; ` ` ` `i--; ` ` ` `} ` ` ` ` ` `// if s[i] == '1' ` ` ` `else` ` ` `{ ` ` ` `count_++; ` ` ` ` ` `// stop until you get 0 in the binary ` ` ` `while` `(s1[i] == ` `'1'` `&& i > 0) ` ` ` `{ ` ` ` `count_++; ` ` ` `i--; ` ` ` `} ` ` ` `if` `(i == 0) ` ` ` `count_++; ` ` ` ` ` `// when encounter a 0 replace it with 1 ` ` ` `s1[i] = ` `'1'` `; ` ` ` `} ` ` ` `} ` ` ` `return` `count_; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String []args) ` ` ` `{ ` ` ` `String s; ` ` ` `s = ` `"10000100000"` `; ` ` ` ` ` `Console.WriteLine(calculate_(s)); ` ` ` `} ` `} ` ` ` `// This code is contributed by princiraj1992 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count the steps ` `// required to convert a number to 1 ` ` ` `// function to calculate the ` `// number of actions ` `function` `calculate_(` `$s` `) ` `{ ` ` ` `// if the size of binary is 1 ` ` ` `// then the number of actions ` ` ` `// will be zero ` ` ` `if` `(` `strlen` `(` `$s` `) == 1) ` ` ` `return` `0; ` ` ` ` ` `// initializing the number of ` ` ` `// actions as 0 at first ` ` ` `$count_` `= 0; ` ` ` `for` `(` `$i` `= ` `strlen` `(` `$s` `) - 1; ` `$i` `> 0;) ` ` ` `{ ` ` ` `// start traversing from the last ` ` ` `// digit if its 0 increment the ` ` ` `// count and decrement i ` ` ` `if` `(` `$s` `[` `$i` `] == ` `'0'` `) ` ` ` `{ ` ` ` `$count_` `++; ` ` ` `$i` `--; ` ` ` `} ` ` ` ` ` `// if $s[$i] == '1' ` ` ` `else` ` ` `{ ` ` ` `$count_` `++; ` ` ` ` ` `// stop until you get 0 in the binary ` ` ` `while` `(` `$s` `[` `$i` `] == ` `'1'` `&& ` `$i` `> 0) ` ` ` `{ ` ` ` `$count_` `++; ` ` ` `$i` `--; ` ` ` `} ` ` ` `if` `(` `$i` `== 0) ` ` ` `$count_` `++; ` ` ` ` ` `// when encounter a 0 replace ` ` ` `// it with 1 ` ` ` `$s` `[` `$i` `] = ` `'1'` `; ` ` ` `} ` ` ` `} ` ` ` `return` `$count_` `; ` `} ` ` ` `// Driver code ` ` ` `$s` `= ` `"10000100000"` `; ` `echo` `calculate_(` `$s` `); ` ` ` `// This code is contributed ` `// by Shivi_Aggarwal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

16

## Recommended Posts:

- Minimum steps to convert one binary string to other only using negation
- Minimum swaps required to convert one binary string to another
- Steps required to visit M points in order on a circular ring of N points
- Minimize steps required to move all 1's in a matrix to a given index
- Minimum operations required to convert a binary string to all 0s or all 1s
- Minimum given operations required to convert a given binary string to all 1's
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Count of minimum reductions required to get the required sum K
- Minimum number of towers required such that every house is in the range of at least one tower
- Minimum number of given operations required to convert a string to another string
- Minimum number of given operation required to convert n to m
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum splits required to convert a number into prime segments
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Minimum steps to remove substring 010 from a binary string
- Minimum number of distinct powers of 2 required to express a given binary number
- Minimum flips required to convert given string into concatenation of equal substrings of length K
- Convert a number of length N such that it contains any one digit at least 'K' times
- Sub-strings that start and end with one character and have at least one other
- Count of strings that become equal to one of the two strings after one removal

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.