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:
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
- Minimum steps to make the product of the array equal to 1
- Number of steps required to convert a binary number to one
- Find the number of stair steps
- Largest number N which can be reduced to 0 in K 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
- 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
- Find minimum number of steps to reach the end of String
- 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
- Find the minimum number to be added to N to make it a prime number
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.