Find Kth smallest value for b such that a + b = a | b | Function to find the kth smallest value for 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 ; i 'th bit is set ; proceed to next bit ; Driver Code
function kthSmallest ( a , k ) { var res = 0 ; var j = 0 ; for ( var i = 0 ; i < 32 ; i ++ ) { while ( j < 32 && ( a & ( 1 << j ) ) ) j ++ ; if ( k & ( 1 << i ) ) res |= ( 1 << j ) ; j ++ ; } return res ; } var a = 10 , k = 3 ; document . write ( kthSmallest ( a , k ) ) ;