Print Value Of Rsb Mask

Try First, Check Solution later

1. You should first read the question and watch the question video.
2. Think of a solution approach, then try and submit the question on editor tab.
3. We strongly advise you to watch the solution video for prescribed approach.

1. You are given a number n.
2. You have to print the right-most set bit mask.
Input Format
A number n
Output Format
A number
Question Video
1 <= n <= 10^9
Sample Input
Sample Output

  • Editorial

    The problem here deals with getting the rightmost set bit of the given input number. For example, n = 58 => [ 1 1 1 0 1 0 ], the answer should be 2 => [ 0 0 0 0 1 0 ] as the rightmost set bit for n is at 1st index.

    To achieve this we need to follow two steps firstly we need to flip the bits of the input number and store it in a mask. Thereafter we need to add 1 to the mask to get our desired mask. AND operation with this mask will yield us the RSB of the given input number.

    Generalised Case for RSB:

    Time Complexity: O(1)

    The time complexity for the function is constant as basic bit operations are involved.

    Space Complexity: O(1)

    The space complexity for the function is constant.

  • Asked in Companies
  • Related Topics

Video Solution

Code Solution

Id Name