Kth smallest positive integer Y such that its sum with X is same as its bitwise OR with X | Function to calculate K - th smallest solution ( Y ) of equation X + Y = X | Y ; Initialize the variable to store the answer ; The i - th bit of X is off ; The i - bit of K is on ; Divide K by 2 ; If K becomes 0 then break ; Driver Code
function KthSolution ( X , K ) { let ans = 0 ; for ( let i = 0 ; i < 64 ; i ++ ) { if ( ! ( X & ( 1 << i ) ) ) { if ( K & 1 ) { ans |= ( 1 << i ) ; } K >>= 1 ; if ( ! K ) { break ; } } } return ans ; } let X = 10 , K = 5 ; document . write ( KthSolution ( X , K ) ) ;