Find Kth smallest value for b such that a + b = a | b | Java program to find k 'th smallest value for b such that a + b = a | b ; Function to find the kth smallest value for b ; res wiint store final answer ; skip when j ' th ▁ position ▁ ▁ has ▁ 1 ▁ in ▁ binary ▁ representation ▁ ▁ as ▁ in ▁ res , ▁ j ' th position wiint be 0. ; j 'th bit is set ; i 'th bit is set ; proceed to next bit ; Driver Code
class GFG { static int kthSmallest ( int a , int k ) { int res = 0 ; int j = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { while ( j < 32 && ( a & ( 1 << j ) ) > 0 ) j ++ ; if ( ( k & ( 1 << i ) ) > 0 ) res |= ( 1 << j ) ; j ++ ; } return res ; } public static void main ( String [ ] args ) { int a = 10 , k = 3 ; System . out . print ( kthSmallest ( a , k ) + "NEW_LINE"); } }