Find K 'th smallest number such that A + B = A | B | Function to find k 'th smallest number such that A + B = A | B ; res will store final answer ; Skip when j ' th ▁ position ▁ ▁ has ▁ 1 ▁ in ▁ binary ▁ representation ▁ ▁ as ▁ in ▁ res , ▁ j ' th position will be 0. ; j 'th bit is set ; If i ' th ▁ bit ▁ of ▁ k ▁ is ▁ 1 ▁ ▁ and ▁ i ' th bit of j is 0 then set i 'th bit in res. ; Proceed to next bit ; Driver Code
function kthSmallest ( a , k ) { let res = 0 ; let j = 0 ; for ( let i = 0 ; i < 32 ; i ++ ) { while ( j < 32 && ( a & ( 1 << j ) ) ) { j ++ ; } if ( k & ( 1 << i ) ) { res |= ( 1 << j ) ; } j ++ ; } return res ; } let a = 5 , k = 3 ; document . write ( kthSmallest ( a , k ) ) ;