K | Python3 program to find k - th term of N merged Arithmetic Progressions ; Function to count and return the number of values less than equal to N present in the set ; Check whether j - th bit is set bit or not ; Function to implement Binary Search to find K - th element ; Find middle index of the array ; Search in the left half ; Search in the right half ; If exactly K elements are present ; Driver Code
maxm = 1000000000 NEW_LINE def count ( v , n ) : NEW_LINE INDENT odd , even = 0 , 0 NEW_LINE t = 1 << len ( v ) NEW_LINE size = len ( v ) NEW_LINE for i in range ( 1 , t ) : NEW_LINE INDENT d , count = 1 , 0 NEW_LINE for j in range ( 0 , size ) : NEW_LINE INDENT if ( i & ( 1 << j ) ) : NEW_LINE INDENT d *= v [ j ] NEW_LINE count += 1 NEW_LINE DEDENT DEDENT if ( count & 1 ) : NEW_LINE INDENT odd += n // d NEW_LINE DEDENT else : NEW_LINE INDENT even += n // d NEW_LINE DEDENT DEDENT return ( odd - even ) NEW_LINE DEDENT def BinarySearch ( l , r , v , key ) : NEW_LINE INDENT while ( r - l > 1 ) : NEW_LINE INDENT mid = ( l + r ) // 2 NEW_LINE if ( key <= count ( v , mid ) ) : NEW_LINE INDENT r = mid NEW_LINE DEDENT else : NEW_LINE INDENT l = mid NEW_LINE DEDENT DEDENT if ( key == count ( v , l ) ) : NEW_LINE INDENT return l NEW_LINE DEDENT else : NEW_LINE INDENT return r NEW_LINE DEDENT DEDENT N , K = 2 , 10 NEW_LINE v = [ 2 , 3 ] NEW_LINE print ( BinarySearch ( 1 , maxm , v , K ) ) NEW_LINE