Given an integer N, the task is to flip all the bits to the left of rightmost set bit and print the number generated.
Input: N = 10
10 (1010 in binary)
flipping all bits left to rightmost set bit (index 2)
-> 6 (0110 in binary)
Input: N = 120
120 (1111000 in binary)
flipping all bits left to rightmost set bit (index 3)
-> 8 (0001000 in binary)
To solve the problem mentioned above we will follow the steps given below:
- Convert the given integer into its binary form and store each bit into an array.
- Traverse the array and break after the first occurrence of 1.
- Flip all bits to the left of that index. Convert that binary sequence into a decimal number and return it.
Time Complexity: O(N)
Auxiliary Space: O(N)
To optimize the above method we will try to use Bitwise operators.
- We are finding the set bit position from the rightmost side that is LSB and the total number of bits.
- XOR the given number with the number which has all set bits having total set bits equal to the total number of bits.
- We don’t want to flip the rightmost bits from a set bit. So we are again taking XOR with the number which has all set bits having total set bits equal to firstbit
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximize the number by flipping at most K bits
- Number formed by the rightmost set bit in N
- Check if all bits can be made same by flipping two consecutive bits
- String formed with middle character of every right substring followed by left sequentially
- Maximum number of consecutive 1s after flipping all 0s in a K length subarray
- Check if bits of a number has count of consecutive set bits in increasing order
- Toggle bits of a number except first and last bits
- Number of possible permutations when absolute difference between number of elements to the right and left are given
- Find smallest number formed by inverting digits of given number N
- Find maximum number that can be formed using digits of a given number
- Find the largest number smaller than integer N with maximum number of set bits
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Maximum number of contiguous array elements with same number of set bits
- Largest even number that can be formed by any number of swaps
- Number of chocolates left after k iterations
- Check if a number has same number of set and unset bits
- Queries to answer the number of ones and zero to the left of given index
- Number of triangles that can be formed with given N points
- Check if a number is formed by Concatenation of 1, 14 or 144 only
- Find the minimum number of rectangles left after inserting one into another
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.