Print steps to make a number in form of 2^X – 1
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 1Pick 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++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the leftmost // unset bit in a number. int find_leftmost_unsetbit(int n) { int ind = -1; int i = 1; while (n) { if (!(n & 1)) ind = i; i++; n >>= 1; } return ind; } // Function that perform // the step void perform_steps(int n) { // Find the leftmost unset bit int left = find_leftmost_unsetbit(n); // If the number has no bit // unset, it means it is in form 2^x -1 if (left == -1) { cout << "No steps required"; return; } // Count the steps int step = 1; // Iterate till number is of form 2^x - 1 while (find_leftmost_unsetbit(n) != -1) { // At even step increase by 1 if (step % 2 == 0) { n += 1; cout << "Step" << step << ": Increase by 1\n"; } // Odd step xor with any 2^m-1 else { // Find the leftmost unset bit int m = find_leftmost_unsetbit(n); // 2^m-1 int num = pow(2, m) - 1; // Perform the step n = n ^ num; cout << "Step" << step << ": Xor with " << num << endl; } // Increase the steps step += 1; } } // Driver code int main() { int n = 39; perform_steps(n); return 0; } |
Java
// Java program to implement // the above approach class GFG { // Function to find the leftmost // unset bit in a number. 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; } // Function that perform // the step static void perform_steps(int n) { // Find the leftmost unset bit int left = find_leftmost_unsetbit(n); // If the number has no bit // unset, it means it is in form 2^x -1 if (left == -1) { System.out.print("No steps required"); return; } // Count the steps int step = 1; // Iterate till number is of form 2^x - 1 while (find_leftmost_unsetbit(n) != -1) { // At even step increase by 1 if (step % 2 == 0) { n += 1; System.out.println("Step" + step + ": Increase by 1"); } // Odd step xor with any 2^m-1 else { // Find the leftmost unset bit int m = find_leftmost_unsetbit(n); // 2^m-1 int num = (int) (Math.pow(2, m) - 1); // Perform the step n = n ^ num; System.out.println("Step" + step + ": Xor with " + num); } // Increase the steps step += 1; } } // Driver code public static void main(String[] args) { int n = 39; perform_steps(n); } } // This code contributed by Rajput-Ji |
Python3
# Python3 program to implement # the above approach # Function to find the leftmost # unset bit in a number. def find_leftmost_unsetbit(n): ind = -1; i = 1; while (n): if ((n % 2) != 1): ind = i; i += 1; n >>= 1; return ind; # Function that perform # the step def perform_steps(n): # Find the leftmost unset bit left = find_leftmost_unsetbit(n); # If the number has no bit # unset, it means it is in form 2^x -1 if (left == -1): print("No steps required"); return; # Count the steps step = 1; # Iterate till number is of form 2^x - 1 while (find_leftmost_unsetbit(n) != -1): # At even step increase by 1 if (step % 2 == 0): n += 1; print("Step" , step , ": Increase by 1\n"); # Odd step xor with any 2^m-1 else: # Find the leftmost unset bit m = find_leftmost_unsetbit(n); # 2^m-1 num = (2**m) - 1; # Perform the step n = n ^ num; print("Step" , step, ": Xor with" , num ); # Increase the steps step += 1; # Driver code n = 39; perform_steps(n); # This code contributed by PrinciRaj1992 |
C#
// C# program to implement // the above approach using System; class GFG { // Function to find the leftmost // unset bit in a number. 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; } // Function that perform // the step static void perform_steps(int n) { // Find the leftmost unset bit int left = find_leftmost_unsetbit(n); // If the number has no bit // unset, it means it is in form 2^x -1 if (left == -1) { Console.Write("No steps required"); return; } // Count the steps int step = 1; // Iterate till number is of form 2^x - 1 while (find_leftmost_unsetbit(n) != -1) { // At even step increase by 1 if (step % 2 == 0) { n += 1; Console.WriteLine("Step" + step + ": Increase by 1"); } // Odd step xor with any 2^m-1 else { // Find the leftmost unset bit int m = find_leftmost_unsetbit(n); // 2^m-1 int num = (int) (Math.Pow(2, m) - 1); // Perform the step n = n ^ num; Console.WriteLine("Step" + step + ": Xor with " + num); } // Increase the steps step += 1; } } // Driver code static public void Main () { int n = 39; perform_steps(n); } } // This code contributed by ajit. |
PHP
<?php // Php program to implement // the above approach // Function to find the leftmost // unset bit in a number. function find_leftmost_unsetbit($n) { $ind = -1; $i = 1; while ($n) { if (!($n & 1)) $ind = $i; $i++; $n >>= 1; } return $ind; } // Function that perform // the step function perform_steps($n) { // Find the leftmost unset bit $left = find_leftmost_unsetbit($n); // If the number has no bit // unset, it means it is in form 2^x -1 if ($left == -1) { echo "No steps required"; return; } // Count the steps $step = 1; // Iterate till number is of form 2^x - 1 while (find_leftmost_unsetbit($n) != -1) { // At even step increase by 1 if ($step % 2 == 0) { $n += 1; echo "Step",$step, ": Increase by 1\n"; } // Odd step xor with any 2^m-1 else { // Find the leftmost unset bit $m = find_leftmost_unsetbit($n); // 2^m-1 $num = pow(2, $m) - 1; // Perform the step $n = $n ^ $num; echo "Step",$step ,": Xor with ", $num ,"\n"; } // Increase the steps $step += 1; } } // Driver code $n = 39; perform_steps($n); // This code is contributed by Ryuga ?> |
Step1: Xor with 31 Step2: Increase by 1 Step3: Xor with 7 Step4: Increase by 1
Recommended Posts:
- Print digit's position to be removed to make a number divisible by 6
- Minimum steps to make the product of the array equal to 1
- Minimum steps to make all the elements of the array divisible by 4
- Number of steps required to convert a binary number to one
- Largest number N which can be reduced to 0 in K steps
- Find the number of stair steps
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Find the minimum number of steps to reach M from N
- Number of steps to convert to prime factors
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Minimize the number of steps required to reach the end of the array
- Minimize the number of steps required to reach the end of the array | Set 2
- Find minimum number to be divided to make a number a perfect square
- Number of digits to be removed to make a number divisible by 3
- Number of subarrays having sum of the form k^m, m >= 0
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.



