19. Rightmost different bit

The problem can be found at the following link: Question Link

My Approach

  • Calculate the bitwise XOR of m and n to find the bits that are different.

  • Find the rightmost set bit in the XOR result using the expression res = res & (-res).

  • Return the position (1-based) of the rightmost set bit.

Time and Auxiliary Space Complexity

  • Time Complexity: O(1)

  • Auxiliary Space Complexity: O(1)

Code (C++)

class Solution {
public:
    int posOfRightMostDiffBit(int m, int n) {
        if (m == n)
            return -1;
            
        int res = m ^ n;
        res = res & (-res);
        return log2(res) + 1;
    }
};

Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.

Last updated