Find Kth smallest value for b such that a + b = a | b | C ++ 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 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long NEW_LINE ll kthSmallest ( ll a , ll k ) { ll res = 0 ; ll j = 0 ; for ( ll i = 0 ; i < 32 ; i ++ ) { while ( j < 32 && ( a & ( 1 << j ) ) ) j ++ ; if ( k & ( 1 << i ) ) res |= ( 1LL << j ) ; j ++ ; } return res ; } int main ( ) { ll a = 10 , k = 3 ; cout << kthSmallest ( a , k ) << " STRNEWLINE " ; return 0 ; }