Find the Kth number which is not divisible by N | Function to find the Kth not divisible by N ; Lowest possible value ; Highest possible value ; To store the Kth non divisible number of N ; Using binary search ; Calculating mid value ; Sol would have the value by subtracting all multiples of n till mid ; Check if sol is greater than k ; H should be reduced to find minimum possible value ; Check if sol is less than k then L will be mid + 1 ; Check if sol is equal to k ; ans will be mid ; H would be reduced to find any more possible value ; Print the answer ; Driver code ; Function Call
function kthNonDivisible ( N , K ) { let L = 1 ; let H = 2147483647 ; let ans = 0 ; while ( L <= H ) { let mid = parseInt ( ( L + H ) / 2 , 10 ) ; let sol = mid - parseInt ( mid / N , 10 ) ; if ( sol > K ) { H = mid - 1 ; } else if ( sol < K ) { L = mid + 1 ; } else { ans = mid ; H = mid - 1 ; } } document . write ( ans ) ; } let N = 3 ; let K = 7 ; kthNonDivisible ( N , K ) ;