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++
#include <bits/stdc++.h>
using namespace std;
int kthNonDivisible(int N, int K)
{
int find = 0;
int j = 0;
while (find != K) {
j++;
if (j % N != 0)
find++;
}
return j;
}
int main()
{
int N = 3;
int K = 6;
cout << kthNonDivisible(N, K);
return 0;
}
|
Java
class GFG{
static int kthNonDivisible(int N, int K)
{
int find = 0;
int j = 0;
while (find != K)
{
j++;
if (j % N != 0)
find++;
}
return j;
}
public static void main(String[] args)
{
int N = 3;
int K = 6;
System.out.print(kthNonDivisible(N, K));
}
}
|
Python3
import math
def kthNonDivisible(n, K):
find = 0
j = 0
while find != K:
j = j + 1
if j % N != 0:
find = find + 1
return j
N = 3
K = 6
print(kthNonDivisible(N, K))
|
C#
using System;
class GFG {
static int kthNonDivisible(int N, int K)
{
int find = 0;
int j = 0;
while (find != K)
{
j++;
if (j % N != 0)
find++;
}
return j;
}
public static void Main(String[] args)
{
int N = 3;
int K = 6;
Console.Write(kthNonDivisible(N, K));
}
}
|
Javascript
<script>
function kthNonDivisible(N, K)
{
let find = 0;
let j = 0;
while (find != K)
{
j++;
if (j % N != 0)
find++;
}
return j;
}
let N = 3;
let K = 6;
document.write(kthNonDivisible(N, K));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
void kthNonDivisible(int N, int K)
{
int L = 1;
int H = INT_MAX;
int ans = 0;
while (L <= H)
{
int mid = (L + H) / 2;
int sol = mid - mid / N;
if (sol > K)
{
H = mid - 1;
}
else if (sol < K)
{
L = mid + 1;
}
else
{
ans = mid;
H = mid - 1;
}
}
cout << ans;
}
int main()
{
int N = 3;
int K = 7;
kthNonDivisible(N, K);
return 0;
}
|
Java
class GFG{
public static void kthNonDivisible(int N,
int K)
{
int L = 1;
int H = Integer.MAX_VALUE;
int ans = 0;
while (L <= H)
{
int mid = (L + H) / 2;
int sol = mid - mid / N;
if (sol > K)
{
H = mid - 1;
}
else if (sol < K)
{
L = mid + 1;
}
else
{
ans = mid;
H = mid - 1;
}
}
System.out.print(ans);
}
public static void main(String[] args)
{
int N = 3;
int K = 7;
kthNonDivisible(N, K);
}
}
|
Python3
import sys
def kthNonDivisible(N, K):
L = 1
H = sys.maxsize
ans = 0
while (L <= H):
mid = (L + H) // 2
sol = mid - mid // N
if (sol > K):
H = mid - 1
elif (sol < K):
L = mid + 1
else:
ans = mid
H = mid - 1
print(ans)
N = 3
K = 7
kthNonDivisible(N, K)
|
C#
using System;
class GFG{
static void kthNonDivisible(int N, int K)
{
int L = 1;
int H = Int32.MaxValue;
int ans = 0;
while (L <= H)
{
int mid = (L + H) / 2;
int sol = mid - mid / N;
if (sol > K)
{
H = mid - 1;
}
else if (sol < K)
{
L = mid + 1;
}
else
{
ans = mid;
H = mid - 1;
}
}
Console.Write(ans);
}
static void Main()
{
int N = 3;
int K = 7;
kthNonDivisible(N, K);
}
}
|
Javascript
<script>
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);
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int kthNonDivisible(int N, int K)
{
return K + floor((K - 1) / (N - 1));
}
int main()
{
int N = 3;
int K = 6;
cout << kthNonDivisible(N, K);
return 0;
}
|
Java
class GFG{
static int kthNonDivisible(int N, int K)
{
return (int) (K + Math.floor((K - 1) / (N - 1)));
}
public static void main(String[] args)
{
int N = 3;
int K = 6;
System.out.print(kthNonDivisible(N, K));
}
}
|
Python3
import math
def kthNonDivisible(N, K):
return K + math.floor((K - 1) / (N - 1))
N = 3
K = 6
print(kthNonDivisible(N, K))
|
C#
using System;
class GFG{
static int kthNonDivisible(int N, int K)
{
return (int) (K + Math.Floor((double)(K - 1) /
(N - 1)));
}
public static void Main(String[] args)
{
int N = 3;
int K = 6;
Console.Write(kthNonDivisible(N, K));
}
}
|
Javascript
<script>
function kthNonDivisible(N, K)
{
return K + parseInt(
Math.floor((K - 1) / (N - 1)), 10);
}
let N = 3;
let K = 6;
document.write(kthNonDivisible(N, K));
</script>
|
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!