Find the Kth number which is not divisible by N - GeeksforGeeks

14 Jun, 2021
Given two integers N and K, the task is to find the Kth number which is not divisible by N.Note: The value of N is greater than 1, because every number is divisible by 1. 
Examples:
Input: N = 3, K = 6 Output: 8 Explanation: Numbers which is not divisible by N = 3 – {1, 2, 4, 5, 7, 8, 10} 6th non-divisible number by 3 is 8. 
Input: N = 7, K = 97 Output: 113 Explanation: Numbers which is not divisible by N = 7 – {1, 2, 4, 5, 6, ....} 97th non-divisible number by 7 is 113.
Naive Approach: A simple solution is to iterate over a loop to find the Kth non-divisible number by N. Below is the steps to find the Kth number: 
Initialize the count of non-divisible number and current number to 0.
Iterate using a while loop until the count of the non-divisible number is not equal to K.
Increment the count of the non-divisible number by 1, If the current number is not divisible by N.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation to find// the K'th non divisible// number by N #include <bits/stdc++.h> using namespace std; // Function to find// the K'th non divisible// number by Nint kthNonDivisible(int N, int K){    int find = 0;    int j = 0;     // Loop to find the K non    // divisible number by N    while (find != K) {        j++;        if (j % N != 0)            find++;    }    return j;} // Driver Codeint main(){    int N = 3;    int K = 6;    cout << kthNonDivisible(N, K);    return 0;}
// Java implementation to find// the K'th non divisible// number by Nclass GFG{ // Function to find// the K'th non divisible// number by Nstatic int kthNonDivisible(int N, int K){    int find = 0;    int j = 0;     // Loop to find the K non    // divisible number by N    while (find != K)    {        j++;        if (j % N != 0)            find++;    }    return j;} // Driver codepublic static void main(String[] args){    int N = 3;    int K = 6;     System.out.print(kthNonDivisible(N, K));}} // This code is contributed by shivanisinghss2110
# Python3 implementation to find# the K'th non divisible# number of Nimport math # Function to find the Kth# not divisible by Ndef kthNonDivisible(n, K):     find = 0    j = 0     # Loop to find the K non    # divisible number by N    while find != K:           j = j + 1        if j % N != 0:            find = find + 1                 return j # Driver CodeN = 3K = 6 # Function Callprint(kthNonDivisible(N, K)) # This code is contributed by ishayadav181
// C# implementation to find the// K'th non-divisible number by Nusing System; class GFG { // Function to find the K'th// non divisible number by Nstatic int kthNonDivisible(int N, int K){    int find = 0;    int j = 0;     // Loop to find the K non    // divisible number by N    while (find != K)    {        j++;                 if (j % N != 0)            find++;    }    return j;} // Driver codepublic static void Main(String[] args){    int N = 3;    int K = 6;     Console.Write(kthNonDivisible(N, K));}} // This code is contributed by shivanisinghss2110
<script>    // Javascript implementation to find the    // K'th non-divisible number by N         // Function to find the K'th    // non divisible number by N    function kthNonDivisible(N, K)    {        let find = 0;        let j = 0;         // Loop to find the K non        // divisible number by N        while (find != K)        {            j++;             if (j % N != 0)                find++;        }        return j;    }           let N = 3;    let K = 6;      document.write(kthNonDivisible(N, K));         // This code is contributed by decode2207.</script>
8
Another Approach – Using Binary Search The idea is to use Binary Search to solve this problem. The search space for this problem will be from 1 to the maximum integer value and the middle value is computed as the difference of the middle of search space and multiples of the N.
If the middle value is greater than K, then update the value of the H as middle-1.
Otherwise, If the middle value is greater than K, then update the value of the L as middle – 1.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ implementation for// above approach#include <bits/stdc++.h>using namespace std; // Function to find the Kth// not divisible by Nvoid kthNonDivisible(int N, int K){         // Lowest possible value    int L = 1;       // Highest possible value    int H = INT_MAX;       // To store the Kth non    // divisible number of N    int ans = 0;     // Using binary search    while (L <= H)    {                 // Calculating mid value        int mid = (L + H) / 2;         // Sol would have the value        // by subtracting all        // multiples of n till mid        int sol = mid - mid / N;         // Check if sol is greater than k        if (sol > K)        {                       // H should be reduced to find            // minimum possible value            H = mid - 1;        }               // Check if sol is less than k        // then L will be mid+1        else if (sol < K)        {            L = mid + 1;        }               // Check if sol is equal to k        else        {                         // ans will be mid            ans = mid;                       // H would be reduced to find any            // more possible value            H = mid - 1;        }    }       // Print the answer    cout << ans;} // Driver Codeint main(){    int N = 3;    int K = 7;     // Function Call    kthNonDivisible(N, K);    return 0;}
// Java implementation for// above approachclass GFG{     // Function to find the Kth// not divisible by Npublic static void kthNonDivisible(int N,                                   int K){         // Lowest possible value    int L = 1;        // Highest possible value    int H = Integer.MAX_VALUE;        // To store the Kth non    // divisible number of N    int ans = 0;      // Using binary search    while (L <= H)    {                 // Calculating mid value        int mid = (L + H) / 2;          // Sol would have the value        // by subtracting all        // multiples of n till mid        int sol = mid - mid / N;          // Check if sol is greater than k        if (sol > K)        {                         // H should be reduced to find            // minimum possible value            H = mid - 1;        }                // Check if sol is less than k        // then L will be mid+1        else if (sol < K)        {            L = mid + 1;        }                // Check if sol is equal to k        else        {                         // ans will be mid            ans = mid;                        // H would be reduced to find any            // more possible value            H = mid - 1;        }    }        // Print the answer    System.out.print(ans);} // Driver codepublic static void main(String[] args){    int N = 3;    int K = 7;         // Function Call    kthNonDivisible(N, K);}} // This code is contributed by divyeshrabadiya07
# Python3 implementation for# above approachimport sys # Function to find the Kth# not divisible by Ndef kthNonDivisible(N, K):         # Lowest possible value    L = 1       # Highest possible value    H = sys.maxsize       # To store the Kth non    # divisible number of N    ans = 0     # Using binary search    while (L <= H):                 # Calculating mid value        mid = (L + H) // 2         # Sol would have the value        # by subtracting all        # multiples of n till mid        sol = mid - mid // N         # Check if sol is greater than k        if (sol > K):                       # H should be reduced to find            # minimum possible value            H = mid - 1               # Check if sol is less than k        # then L will be mid+1        elif (sol < K):          L = mid + 1               # Check if sol is equal to k        else:                         # ans will be mid            ans = mid                       # H would be reduced to find any            # more possible value            H = mid - 1       # Print the answer    print(ans) # Driver CodeN = 3K = 7 # Function callkthNonDivisible(N, K) # This code is contributed by ANKITKUMAR34
// C# implementation for// above approachusing System; class GFG{     // Function to find the Kth// not divisible by Nstatic void kthNonDivisible(int N, int K){         // Lowest possible value    int L = 1;         // Highest possible value    int H = Int32.MaxValue;         // To store the Kth non    // divisible number of N    int ans = 0;       // Using binary search    while (L <= H)    {                 // Calculating mid value        int mid = (L + H) / 2;           // Sol would have the value        // by subtracting all        // multiples of n till mid        int sol = mid - mid / N;           // Check if sol is greater than k        if (sol > K)        {                         // H should be reduced to find            // minimum possible value            H = mid - 1;        }                 // Check if sol is less than k        // then L will be mid+1        else if (sol < K)        {            L = mid + 1;        }                 // Check if sol is equal to k        else        {                         // ans will be mid            ans = mid;                         // H would be reduced to find            // any more possible value            H = mid - 1;        }    }         // Print the answer    Console.Write(ans);} // Driver codestatic void Main(){    int N = 3;    int K = 7;          // Function Call    kthNonDivisible(N, K);}} // This code is contributed by divyesh072019
<script>    // Javascript implementation for above approach         // Function to find the Kth    // not divisible by N    function kthNonDivisible(N, K)    {         // Lowest possible value        let L = 1;         // Highest possible value        let H = 2147483647;         // To store the Kth non        // divisible number of N        let ans = 0;         // Using binary search        while (L <= H)        {             // Calculating mid value            let mid = parseInt((L + H) / 2, 10);             // Sol would have the value            // by subtracting all            // multiples of n till mid            let sol = mid - parseInt(mid / N, 10);             // Check if sol is greater than k            if (sol > K)            {                 // H should be reduced to find                // minimum possible value                H = mid - 1;            }             // Check if sol is less than k            // then L will be mid+1            else if (sol < K)            {                L = mid + 1;            }             // Check if sol is equal to k            else            {                 // ans will be mid                ans = mid;                 // H would be reduced to find                // any more possible value                H = mid - 1;            }        }         // Print the answer        document.write(ans);    }         let N = 3;    let K = 7;           // Function Call    kthNonDivisible(N, K); // This code is contributed by mukesh07.</script>
10
Time Complexity: O(logN)
Efficient Approach: The key observation in the problem is that every number from 1 to N-1 is not divisible by N and then Similarly, N + 1 to 2*N – 1 is also not divisible by N. Keeping this in mind, the Kth number not divisible by N will be:
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation to find// the K'th non-divisible// number of N #include <bits/stdc++.h> using namespace std;// Function to find the Kth// not divisible by Nint kthNonDivisible(int N, int K){    return K + floor((K - 1) / (N - 1));} // Driver Codeint main(){    int N = 3;    int K = 6;     // Function Call    cout << kthNonDivisible(N, K);    return 0;}
// Java implementation to find the// K'th non-divisible number of Nclass GFG{     // Function to find the Kth// not divisible by Nstatic int kthNonDivisible(int N, int K){    return (int) (K + Math.floor((K - 1) / (N - 1)));} // Driver Codepublic static void main(String[] args){    int N = 3;    int K = 6;     // Function Call    System.out.print(kthNonDivisible(N, K));}} // This code is contributed by amal kumar choubey
# Python3 implementation to find# the K'th non-divisible# number of Nimport math # Function to find the Kth# not divisible by Ndef kthNonDivisible(N, K):         return K + math.floor((K - 1) / (N - 1))     # Driver CodeN = 3K = 6 # Function Callprint(kthNonDivisible(N, K)) # This code is contributed by ishayadav181
// C# implementation to find the// K'th non-divisible number of Nusing System; class GFG{     // Function to find the Kth// not divisible by Nstatic int kthNonDivisible(int N, int K){    return (int) (K + Math.Floor((double)(K - 1) /                                         (N - 1)));} // Driver Codepublic static void Main(String[] args){    int N = 3;    int K = 6;     // Function Call    Console.Write(kthNonDivisible(N, K));}} // This code is contributed by amal kumar choubey
<script> // Javascript implementation to find// the K'th non-divisible// number of N   // Function to find the Kth// not divisible by Nfunction kthNonDivisible(N, K){    return K + parseInt(        Math.floor((K - 1) / (N - 1)), 10);} // Driver codelet N = 3;let K = 6; // Function Calldocument.write(kthNonDivisible(N, K)); // This code is contributed by suresh07 </script>
8
ishayadav181
shivanisinghss2110
Amal Kumar Choubey
mishrapriyanshu557
ANKITKUMAR34
khushboogoyal499
divyeshrabadiya07
divyesh072019
suresh07
decode2207
mukesh07
divisibility
Numbers
Competitive Programming
Mathematical
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Prefix Sum Array - Implementation and Applications in Competitive Programming
Bits manipulation (Important tactics)
Formatted output in Java
Algorithm Library | C++ Magicians STL Algorithm
How to begin with Competitive Programming?
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7