Minimum difference between any two primes from the given range
Given two integers L and R, the task is to find the minimum difference between any two prime numbers in the range [L, R].
Examples:
Input: L = 21, R = 50
Output: 2
(29, 31) and (41, 43) are the only valid pairs
that give the minimum difference.
Input: L = 1, R = 11
Output: 1
The difference between (2, 3) is minimum.
Approach:
- Find all the prime numbers upto R using Sieve of Eratosthenes.
- Now starting from L, find the difference between any two prime numbers within the range and update minimum difference so far.
- If the number of primes in the range were < 2 then print -1.
- Else print the minimum difference.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
const int sz = 1e5;
bool isPrime[sz + 1];
void sieve()
{
memset (isPrime, true , sizeof (isPrime));
isPrime[0] = isPrime[1] = false ;
for ( int i = 2; i * i <= sz; i++) {
if (isPrime[i]) {
for ( int j = i * i; j < sz; j += i) {
isPrime[j] = false ;
}
}
}
}
int minDifference( int L, int R)
{
int fst = 0;
for ( int i = L; i <= R; i++) {
if (isPrime[i]) {
fst = i;
break ;
}
}
int snd = 0;
for ( int i = fst + 1; i <= R; i++) {
if (isPrime[i]) {
snd = i;
break ;
}
}
if (snd == 0)
return -1;
int diff = snd - fst;
int left = snd + 1;
int right = R;
for ( int i = left; i <= right; i++) {
if (isPrime[i]) {
if (i - snd <= diff) {
fst = snd;
snd = i;
diff = snd - fst;
}
}
}
return diff;
}
int main()
{
sieve();
int L = 21, R = 50;
cout << minDifference(L, R);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int sz = ( int ) 1e5;
static boolean []isPrime = new boolean [sz + 1 ];
static void sieve()
{
Arrays.fill(isPrime, true );
isPrime[ 0 ] = isPrime[ 1 ] = false ;
for ( int i = 2 ; i * i <= sz; i++)
{
if (isPrime[i])
{
for ( int j = i * i; j < sz; j += i)
{
isPrime[j] = false ;
}
}
}
}
static int minDifference( int L, int R)
{
int fst = 0 ;
for ( int i = L; i <= R; i++)
{
if (isPrime[i])
{
fst = i;
break ;
}
}
int snd = 0 ;
for ( int i = fst + 1 ; i <= R; i++)
{
if (isPrime[i])
{
snd = i;
break ;
}
}
if (snd == 0 )
return - 1 ;
int diff = snd - fst;
int left = snd + 1 ;
int right = R;
for ( int i = left; i <= right; i++)
{
if (isPrime[i])
{
if (i - snd <= diff)
{
fst = snd;
snd = i;
diff = snd - fst;
}
}
}
return diff;
}
public static void main(String []args)
{
sieve();
int L = 21 , R = 50 ;
System.out.println(minDifference(L, R));
}
}
|
Python3
from math import sqrt
sz = int ( 1e5 );
isPrime = [ True ] * (sz + 1 );
def sieve() :
isPrime[ 0 ] = isPrime[ 1 ] = False ;
for i in range ( 2 , int (sqrt(sz)) + 1 ) :
if (isPrime[i]) :
for j in range (i * i, sz, i) :
isPrime[j] = False ;
def minDifference(L, R) :
fst = 0 ;
for i in range (L, R + 1 ) :
if (isPrime[i]) :
fst = i;
break ;
snd = 0 ;
for i in range (fst + 1 , R + 1 ) :
if (isPrime[i]) :
snd = i;
break ;
if (snd = = 0 ) :
return - 1 ;
diff = snd - fst;
left = snd + 1 ;
right = R;
for i in range (left, right + 1 ) :
if (isPrime[i]) :
if (i - snd < = diff) :
fst = snd;
snd = i;
diff = snd - fst;
return diff;
if __name__ = = "__main__" :
sieve();
L = 21 ; R = 50 ;
print (minDifference(L, R));
|
C#
using System;
class GFG
{
static int sz = ( int ) 1e5;
static Boolean []isPrime = new Boolean [sz + 1];
static void sieve()
{
for ( int i = 2; i< sz + 1; i++)
isPrime[i] = true ;
for ( int i = 2; i * i <= sz; i++)
{
if (isPrime[i])
{
for ( int j = i * i; j < sz; j += i)
{
isPrime[j] = false ;
}
}
}
}
static int minDifference( int L, int R)
{
int fst = 0;
for ( int i = L; i <= R; i++)
{
if (isPrime[i])
{
fst = i;
break ;
}
}
int snd = 0;
for ( int i = fst + 1; i <= R; i++)
{
if (isPrime[i])
{
snd = i;
break ;
}
}
if (snd == 0)
return -1;
int diff = snd - fst;
int left = snd + 1;
int right = R;
for ( int i = left; i <= right; i++)
{
if (isPrime[i])
{
if (i - snd <= diff)
{
fst = snd;
snd = i;
diff = snd - fst;
}
}
}
return diff;
}
public static void Main(String []args)
{
sieve();
int L = 21, R = 50;
Console.WriteLine(minDifference(L, R));
}
}
|
Javascript
<script>
const sz = 1e5;
let isPrime = new Array(sz + 1);
function sieve()
{
isPrime.fill( true );
isPrime[0] = isPrime[1] = false ;
for (let i = 2; i * i <= sz; i++)
{
if (isPrime[i])
{
for (let j = i * i; j < sz; j += i)
{
isPrime[j] = false ;
}
}
}
}
function minDifference(L, R)
{
let fst = 0;
for (let i = L; i <= R; i++)
{
if (isPrime[i])
{
fst = i;
break ;
}
}
let snd = 0;
for (let i = fst + 1; i <= R; i++)
{
if (isPrime[i])
{
snd = i;
break ;
}
}
if (snd == 0)
return -1;
let diff = snd - fst;
let left = snd + 1;
let right = R;
for (let i = left; i <= right; i++)
{
if (isPrime[i])
{
if (i - snd <= diff)
{
fst = snd;
snd = i;
diff = snd - fst;
}
}
}
return diff;
}
sieve();
let L = 21, R = 50;
document.write(minDifference(L, R));
</script>
|
Time Complexity: O((R – L) + sqrt(105))
Auxiliary Space: O(105)
Last Updated :
08 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...