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
def kthSmallest ( a , k ) : NEW_LINE INDENT res = 0 NEW_LINE j = 0 NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT while ( j < 32 and ( a & ( 1 << j ) ) ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if ( k & ( 1 << i ) ) : NEW_LINE INDENT res |= ( 1 << j ) NEW_LINE DEDENT j += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT a = 5 NEW_LINE k = 3 NEW_LINE print ( kthSmallest ( a , k ) ) NEW_LINE