Number of steps required to convert a binary number to one
Last Updated :
29 Feb, 2024
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 having odd value, add 1 to it.
- If ‘S’ is having even value, divide it by 2.
Examples:
Input: str = “1001001”
Output: 12
Input: str = “101110”
Output: 8
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++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int calculate_(string s)
{
if (s.size() == 1)
return 0;
int count_ = 0;
for ( int i = s.length() - 1; i > 0;) {
if (s[i] == '0' ) {
count_++;
i--;
}
else {
count_++;
while (s[i] == '1' && i > 0) {
count_++;
i--;
}
if (i == 0)
count_++;
s[i] = '1' ;
}
}
return count_;
}
int main()
{
string s;
s = "10000100000" ;
cout << calculate_(s);
return 0;
}
|
Java
public class ACX {
static int calculate_(String s)
{
if (s.length() == 1 )
return 0 ;
int count_ = 0 ;
char [] s1=s.toCharArray();
for ( int i = s.length() - 1 ; i > 0 😉 {
if (s1[i] == '0' ) {
count_++;
i--;
}
else {
count_++;
while (s1[i] == '1' && i > 0 ) {
count_++;
i--;
}
if (i == 0 )
count_++;
s1[i] = '1' ;
}
}
return count_;
}
public static void main(String []args)
{
String s;
s = "10000100000" ;
System.out.println(calculate_(s));
}
}
|
C#
using System;
class GFG
{
static int calculate_(String s)
{
if (s.Length == 1)
return 0;
int count_ = 0;
char [] s1 = s.ToCharArray();
for ( int i = s.Length - 1; i > 0;)
{
if (s1[i] == '0' )
{
count_++;
i--;
}
else
{
count_++;
while (s1[i] == '1' && i > 0)
{
count_++;
i--;
}
if (i == 0)
count_++;
s1[i] = '1' ;
}
}
return count_;
}
public static void Main(String []args)
{
String s;
s = "10000100000" ;
Console.WriteLine(calculate_(s));
}
}
|
Javascript
<script>
function calculate_(s)
{
if (s.length == 1)
return 0;
let count_ = 0;
let s1 = s.split( "" )
for (let i = s.length - 1; i > 0;)
{
if (s1[i] == '0' )
{
count_++;
i--;
}
else
{
count_++;
while (s1[i] == '1' && i > 0)
{
count_++;
i--;
}
if (i == 0)
count_++;
s1[i] = '1' ;
}
}
return count_;
}
let s = "10000100000" ;
document.write(calculate_(s));
</script>
|
PHP
<?php
function calculate_( $s )
{
if ( strlen ( $s ) == 1)
return 0;
$count_ = 0;
for ( $i = strlen ( $s ) - 1; $i > 0;)
{
if ( $s [ $i ] == '0' )
{
$count_ ++;
$i --;
}
else
{
$count_ ++;
while ( $s [ $i ] == '1' && $i > 0)
{
$count_ ++;
$i --;
}
if ( $i == 0)
$count_ ++;
$s [ $i ] = '1' ;
}
}
return $count_ ;
}
$s = "10000100000" ;
echo calculate_( $s );
?>
|
Python3
def calculate_(s):
if len (s) = = 1 :
return 0
count_ = 0
i = len (s) - 1
while i > 0 :
if s[i] = = '0' :
count_ + = 1
i - = 1
else :
count_ + = 1
while s[i] = = '1' and i > 0 :
count_ + = 1
i - = 1
if i = = 0 :
count_ + = 1
s = s[:i] + "1" + s[i + 1 :]
return count_
s = "10000100000"
print (calculate_(s))
|
Time complexity: O(n) where n is the length of binary string
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...