Kth smallest Prime Number in range L to R for Q queries
Last Updated :
11 Jan, 2022
Given three variables L, R and Q which denotes the range [L, R] and total number of queries. For each query there will be a variable K. The task is to find Kth smallest prime number in range [L, R]. If K is greater than count of prime numbers in the range [L, R] then return -1.
Examples:
Input: Q = 3, L = 5, R = 20
queries: K = 4,
K = 3,
K = 9
Output: 13 11 -1
Explanation: The prime numbers in the given range are 5, 7, 11, 13, 17, 19 and
the 4th and 3rd smallest prime numbers are 13 and 11.
Input: Q = 1, L = 15, R = 32
queries: K = 3
Output: 23
Approach: The given problem can be solved with the help of Sieve of Eratosthenes method:
When the algorithm terminates, all the numbers in the list that are not marked are prime. Follow the steps mentioned below:
- Run Sieve of Eratosthenes for number in range [L, R]
- For each query find the Kth smallest prime from the calculated primes.
- If K exceeds the number of primes in that range, return -1.
See the illustration below for better understanding of Sieve of Eratosthenes.
Illustration: Take range [20, 40].
Sieve of Eratosthenes will help in finding the primes in this range.
Create a list of all numbers from 1 to 40. According to the algorithm mark all the non-prime numbers in the given range.
So the prime numbers are the unmarked ones: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37.
The primes in range [20, 40] are: 23, 29, 31 and 37
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > primes;
void sieveOfEratosthenes( int R)
{
primes.assign(R + 1, 0);
primes[1] = 1;
for ( int i = 2; i * i <= R; i++) {
if (primes[i] == 0) {
for ( int j = i + i; j <= R;
j += i)
primes[j] = 1;
}
}
}
int kthSmallest( int L, int R, int K)
{
int count = 0;
for ( int i = L; i <= R; i++) {
if (primes[i] == 0) {
count++;
}
if (count == K) {
return i;
}
}
return -1;
}
int main()
{
int Q = 3;
int L, R, K;
L = 5, R = 20;
sieveOfEratosthenes(R);
K = 4;
cout << kthSmallest(L, R, K) << endl;
K = 3;
cout << kthSmallest(L, R, K) << endl;
K = 5;
cout << kthSmallest(L, R, K) << endl;
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int primes[] = new int [ 1000 ];
static void sieveOfEratosthenes( int R)
{
for ( int i = 0 ; i < R + 1 ; i++) {
primes[i] = 0 ;
}
primes[ 1 ] = 1 ;
for ( int i = 2 ; i * i <= R; i++) {
if (primes[i] == 0 ) {
for ( int j = i + i; j <= R;
j += i)
primes[j] = 1 ;
}
}
}
static int kthSmallest( int L, int R, int K)
{
int count = 0 ;
for ( int i = L; i <= R; i++) {
if (primes[i] == 0 ) {
count++;
}
if (count == K) {
return i;
}
}
return - 1 ;
}
public static void main(String args[])
{
int Q = 3 ;
int L = 5 , R = 20 ;
sieveOfEratosthenes(R);
int K = 4 ;
System.out.println(kthSmallest(L, R, K));
K = 3 ;
System.out.println(kthSmallest(L, R, K));
K = 5 ;
System.out.println(kthSmallest(L, R, K));
}
}
|
Python3
primes = [ 0 for i in range ( 1000 )];
def sieveOfEratosthenes(R):
for i in range ( 0 , R + 1 ):
primes[i] = 0 ;
primes[ 1 ] = 1 ;
for i in range ( 2 , pow (R, 2 ) + 1 ):
if (primes[i] = = 0 ):
for j in range (i + i, R + 1 , i):
primes[j] = 1 ;
def kthSmallest(L, R, K):
count = 0 ;
for i in range (L,R + 1 ):
if (primes[i] = = 0 ):
count + = 1 ;
if (count = = K):
return i;
return - 1 ;
if __name__ = = '__main__' :
Q = 3 ;
L = 5 ; R = 20 ;
sieveOfEratosthenes(R);
K = 4 ;
print (kthSmallest(L, R, K));
K = 3 ;
print (kthSmallest(L, R, K));
K = 5 ;
print (kthSmallest(L, R, K));
|
C#
using System;
class GFG
{
static int []primes = new int [1000];
static void sieveOfEratosthenes( int R)
{
for ( int i = 0; i < R + 1; i++) {
primes[i] = 0;
}
primes[1] = 1;
for ( int i = 2; i * i <= R; i++) {
if (primes[i] == 0) {
for ( int j = i + i; j <= R;
j += i)
primes[j] = 1;
}
}
}
static int kthSmallest( int L, int R, int K)
{
int count = 0;
for ( int i = L; i <= R; i++) {
if (primes[i] == 0) {
count++;
}
if (count == K) {
return i;
}
}
return -1;
}
public static void Main()
{
int Q = 3;
int L = 5, R = 20;
sieveOfEratosthenes(R);
int K = 4;
Console.WriteLine(kthSmallest(L, R, K));
K = 3;
Console.WriteLine(kthSmallest(L, R, K));
K = 5;
Console.WriteLine(kthSmallest(L, R, K));
}
}
|
Javascript
<script>
let primes;
function sieveOfEratosthenes(R)
{
primes = new Array(R + 1).fill(0);
primes[1] = 1;
for (let i = 2; i * i <= R; i++)
{
if (primes[i] == 0)
{
for (let j = i + i; j <= R;
j += i)
primes[j] = 1;
}
}
}
function kthSmallest(L, R, K)
{
let count = 0;
for (let i = L; i <= R; i++)
{
if (primes[i] == 0)
{
count++;
}
if (count == K)
{
return i;
}
}
return -1;
}
let Q = 3;
let L, R, K;
L = 5, R = 20;
sieveOfEratosthenes(R);
K = 4;
document.write(kthSmallest(L, R, K) + '<br>' );
K = 3;
document.write(kthSmallest(L, R, K) + '<br>' );
K = 5;
document.write(kthSmallest(L, R, K) + '<br>' );
</script>
|
Time Complexity: O(N * log(log N)))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...