K | Javascript 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
let maxm = 1000000000 ; function count ( v , n ) { let i , odd = 0 , even = 0 ; let j , d , count ; let t = 1 << v . length ; let size = v . length ; for ( i = 1 ; i < t ; i ++ ) { d = 1 ; count = 0 ; for ( j = 0 ; j < size ; j ++ ) { if ( ( i & ( 1 << j ) ) > 0 ) { d *= v [ j ] ; count ++ ; } } if ( count % 2 == 1 ) odd += n / d ; else even += n / d ; } return ( odd - even ) ; } function BinarySearch ( l , r , v , key ) { let mid ; while ( r - l > 1 ) { mid = Math . floor ( ( l + r ) / 2 ) ; if ( key <= count ( v , mid ) ) { r = mid ; } else { l = mid ; } } if ( key == count ( v , l ) ) return l ; else return r ; } let N = 2 , K = 10 ; let v = [ 2 , 3 ] ; document . write ( BinarySearch ( 1 , maxm , v , K ) + " " ) ;