Print steps to make a number in form of 2^X – 1
Last Updated :
21 Jun, 2022
Given a number N, there are two steps to be performed.
- At odd step, XOR the number with any 2^M-1, where M is chosen by you.
- At even step, increase the number by 1.
Keep performing the steps until N becomes 2^X-1 (where x can be any integer). The task is to print all the steps.
Examples:
Input: 39
Output:
Step1: Xor with 31
Step2: Increase by 1
Step3: Xor with 7
Step4: Increase by 1
Pick M = 5, N is transformed into 39 ^ 31 = 56.
Increase N by 1 changing its value to N = 57.
Pick M = 3, x is transformed into 57 ^ 7 = 62.
Increase X by 1, changing its value to 63 = 2^6 – 1.
Input: 7
Output: No steps required.
Approach: The following steps can be followed to solve the above problem:
- At every odd step, find the leftmost unset bit (say position x in terms of 1 indexing) in the number and do a xor with 2^x-1.
- At every even step, increase the number by 1.
- If at any step, the number has no more unset bits, then return.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int find_leftmost_unsetbit( int n)
{
int ind = -1;
int i = 1;
while (n) {
if (!(n & 1))
ind = i;
i++;
n >>= 1;
}
return ind;
}
void perform_steps( int n)
{
int left = find_leftmost_unsetbit(n);
if (left == -1) {
cout << "No steps required" ;
return ;
}
int step = 1;
while (find_leftmost_unsetbit(n) != -1) {
if (step % 2 == 0) {
n += 1;
cout << "Step" << step << ": Increase by 1\n" ;
}
else {
int m = find_leftmost_unsetbit(n);
int num = pow (2, m) - 1;
n = n ^ num;
cout << "Step" << step
<< ": Xor with " << num << endl;
}
step += 1;
}
}
int main()
{
int n = 39;
perform_steps(n);
return 0;
}
|
Java
class GFG
{
static int find_leftmost_unsetbit( int n)
{
int ind = - 1 ;
int i = 1 ;
while (n > 0 )
{
if ((n % 2 ) != 1 )
{
ind = i;
}
i++;
n >>= 1 ;
}
return ind;
}
static void perform_steps( int n)
{
int left = find_leftmost_unsetbit(n);
if (left == - 1 )
{
System.out.print( "No steps required" );
return ;
}
int step = 1 ;
while (find_leftmost_unsetbit(n) != - 1 )
{
if (step % 2 == 0 )
{
n += 1 ;
System.out.println( "Step" + step + ": Increase by 1" );
}
else
{
int m = find_leftmost_unsetbit(n);
int num = ( int ) (Math.pow( 2 , m) - 1 );
n = n ^ num;
System.out.println( "Step" + step
+ ": Xor with " + num);
}
step += 1 ;
}
}
public static void main(String[] args)
{
int n = 39 ;
perform_steps(n);
}
}
|
Python3
def find_leftmost_unsetbit(n):
ind = - 1 ;
i = 1 ;
while (n):
if ((n % 2 ) ! = 1 ):
ind = i;
i + = 1 ;
n >> = 1 ;
return ind;
def perform_steps(n):
left = find_leftmost_unsetbit(n);
if (left = = - 1 ):
print ( "No steps required" );
return ;
step = 1 ;
while (find_leftmost_unsetbit(n) ! = - 1 ):
if (step % 2 = = 0 ):
n + = 1 ;
print ( "Step" , step ,
": Increase by 1\n" );
else :
m = find_leftmost_unsetbit(n);
num = ( 2 * * m) - 1 ;
n = n ^ num;
print ( "Step" , step,
": Xor with" , num );
step + = 1 ;
n = 39 ;
perform_steps(n);
|
C#
using System;
class GFG
{
static int find_leftmost_unsetbit( int n)
{
int ind = -1;
int i = 1;
while (n > 0)
{
if ((n % 2) != 1)
{
ind = i;
}
i++;
n >>= 1;
}
return ind;
}
static void perform_steps( int n)
{
int left = find_leftmost_unsetbit(n);
if (left == -1)
{
Console.Write( "No steps required" );
return ;
}
int step = 1;
while (find_leftmost_unsetbit(n) != -1)
{
if (step % 2 == 0)
{
n += 1;
Console.WriteLine( "Step" + step + ": Increase by 1" );
}
else
{
int m = find_leftmost_unsetbit(n);
int num = ( int ) (Math.Pow(2, m) - 1);
n = n ^ num;
Console.WriteLine( "Step" + step
+ ": Xor with " + num);
}
step += 1;
}
}
static public void Main ()
{
int n = 39;
perform_steps(n);
}
}
|
PHP
<?php
function find_leftmost_unsetbit( $n )
{
$ind = -1;
$i = 1;
while ( $n )
{
if (!( $n & 1))
$ind = $i ;
$i ++;
$n >>= 1;
}
return $ind ;
}
function perform_steps( $n )
{
$left = find_leftmost_unsetbit( $n );
if ( $left == -1)
{
echo "No steps required" ;
return ;
}
$step = 1;
while (find_leftmost_unsetbit( $n ) != -1)
{
if ( $step % 2 == 0)
{
$n += 1;
echo "Step" , $step , ": Increase by 1\n" ;
}
else
{
$m = find_leftmost_unsetbit( $n );
$num = pow(2, $m ) - 1;
$n = $n ^ $num ;
echo "Step" , $step , ": Xor with " , $num , "\n" ;
}
$step += 1;
}
}
$n = 39;
perform_steps( $n );
?>
|
Javascript
<script>
function find_leftmost_unsetbit(n)
{
let ind = -1;
let i = 1;
while (n) {
if (!(n & 1))
ind = i;
i++;
n >>= 1;
}
return ind;
}
function perform_steps(n)
{
let left = find_leftmost_unsetbit(n);
if (left == -1) {
document.write( "No steps required" );
return ;
}
let step = 1;
while (find_leftmost_unsetbit(n) != -1) {
if (step % 2 == 0) {
n += 1;
document.write(
"Step" + step + ": Increase by 1<br>"
);
}
else {
let m = find_leftmost_unsetbit(n);
let num = Math.pow(2, m) - 1;
n = n ^ num;
document.write( "Step" + step
+ ": Xor with " + num + "<br>" );
}
step += 1;
}
}
let n = 39;
perform_steps(n);
</script>
|
Output:
Step1: Xor with 31
Step2: Increase by 1
Step3: Xor with 7
Step4: Increase by 1
Time Complexity: O(logN*logN), as we are using a loop to traverse logN times and in each traversal we are calling the function find_leftmost_unsetbit which costs logN.
Auxiliary Space: O(1), as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...