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.
Input: str = “1001001”
Input: str = “101110”
Number ‘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# program to count the steps
//required to convert a number to 1
// 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)
// 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’)
// if s[i] == ‘1’
// stop until you get 0 in the binary
while (s1[i] == ‘1’ && i > 0)
if (i == 0)
// when encounter a 0 replace it with 1
s1[i] = ‘1’;
// Driver code
public static void Main(String args)
s = “10000100000”;
// This code is contributed by princiraj1992
- Minimum number of given operation required to convert n to m
- Minimum steps to convert one binary string to other only using negation
- Minimum number of operations required to sum to binary string S
- Minimum swaps required to convert one binary string to another
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of given operations required to convert a string to another string
- Minimum number of given powers of 2 required to represent a number
- Largest number N which can be reduced to 0 in K steps
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Print steps to make a number in form of 2^X - 1
- Total number of possible Binary Search Trees using Catalan Number
- Binary representation of next greater number with same number of 1's and 0's
- Minimum number of operations required to reduce N to 1
- Count the number of carry operations required to add two numbers
- Minimum number of given moves required to make N divisible by 25
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.