Given an integer N, the task is to find an integer M formed by taking the rightmost set bit in N i.e. the only set bit in M will be the rightmost set bit in N nd the rest of the bits will be unset.
Input: N = 7
7 = 111, the number formed by the last set bit is 001 i.e. 1.
Input: N = 10
10 = 1010 -> 0010 = 2
Input: N = 16
- Store x = n & (n – 1) which will unset the first set bit from the right in n.
- Now, update n = n ^ x to set the changed bit and unset all the others which is the required integer.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Number formed by flipping all bits to the left of rightmost set bit
- Find maximum number that can be formed using digits of a given number
- Largest even number that can be formed by any number of swaps
- Check if a number is formed by Concatenation of 1, 14 or 144 only
- Number of triangles that can be formed with given N points
- Check if the large number formed is divisible by 41 or not
- Length of the smallest number which is divisible by K and formed by using 1's only
- Maximum number of teams that can be formed with given persons
- Greatest number that can be formed from a pair in a given Array
- Greatest number less than equal to B that can be formed from the digits of A
- Number of triangles formed from a set of points on three lines
- Find the largest number that can be formed by changing at most K digits
- Recursive sum of digits of a number formed by repeated appends
- Find Nth even length palindromic number formed using digits X and Y
- Number formed by adding product of its max and min digit K times
- Check if the number formed by the last digits of N numbers is divisible by 10 or not
- Minimum number of consecutive sequences that can be formed in an array
- Number of unique rectangles formed using N unit squares
- Maximum number of line intersections formed through intersection of N planes
- Number formed after K times repeated addition of smallest divisor of N
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.