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.
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.
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:
# Python3 program to implement
# the above approach
# Function to find the leftmost
# unset bit in a number.
ind = -1;
i = 1;
if ((n % 2) != 1):
ind = i;
i += 1;
n >>= 1;
# Function that perform
# the step
# 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”);
# 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
# Find the leftmost unset bit
m = find_leftmost_unsetbit(n);
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;
# This code contributed by PrinciRaj1992
Step1: Xor with 31 Step2: Increase by 1 Step3: Xor with 7 Step4: Increase by 1
- Print digit's position to be removed to make a number divisible by 6
- 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 odd and even results for every value of x in range [min, max] after performing N steps
- Find the minimum number of steps to reach M from N
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Number of steps to convert to prime factors
- 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
- Check whether a very large number of the given form is a multiple of 3.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.