Difference between the largest and the smallest primes in an array
Last Updated :
06 Sep, 2022
Given an array of integers where all the elements are less than 10^6.
The task is to find the difference between the largest and the smallest prime numbers in the array.
Examples:
Input : Array = 1, 2, 3, 5
Output : Difference is 3
Explanation :
The largest prime number in the array is 5 and the smallest is 2
So, the difference is 3
Input : Array = 3, 5, 11, 17
Output : Difference is 14
A Simple approach:
In the basic approach, we will check every element of the array whether it is prime or not. Then, select the largest and the smallest prime numbers and print the difference.
Efficient approach: The efficient approach is much similar to the basic approach.
We will try to reduce the time for checking the number against prime by creating a Sieve of Eratosthenes to check whether the number is prime or not in O(1) time.
And then, we will select the largest and the smallest prime numbers and print the difference.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000000
bool prime[MAX + 1];
void SieveOfEratosthenes()
{
memset (prime, true , sizeof (prime));
prime[1] = false ;
for ( int p = 2; p * p <= MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
}
int findDiff( int arr[], int n)
{
int min = MAX + 2, max = -1;
for ( int i = 0; i < n; i++) {
if (prime[arr[i]] == true ) {
if (arr[i] > max)
max = arr[i];
if (arr[i] < min)
min = arr[i];
}
}
return (max == -1) ? -1 : (max - min);
}
int main()
{
SieveOfEratosthenes();
int n = 4;
int arr[n] = { 1, 2, 3, 5 };
int res = findDiff(arr, n);
if (res == -1)
cout << "No prime numbers" << endl;
else
cout << "Difference is " << res << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int MAX = 1000000 ;
static boolean prime[] = new boolean [MAX + 1 ];
static void SieveOfEratosthenes()
{
for ( int i= 0 ;i<MAX+ 1 ;i++)
prime[i] = true ;
prime[ 1 ] = false ;
for ( int p = 2 ; p * p <= MAX; p++) {
if (prime[p] == true ) {
for ( int i = p * 2 ; i <= MAX; i += p)
prime[i] = false ;
}
}
}
static int findDiff( int arr[], int n)
{
int min = MAX + 2 , max = - 1 ;
for ( int i = 0 ; i < n; i++) {
if (prime[arr[i]] == true ) {
if (arr[i] > max)
max = arr[i];
if (arr[i] < min)
min = arr[i];
}
}
return (max == - 1 )? - 1 : (max - min);
}
public static void main (String[] args) {
SieveOfEratosthenes();
int n = 4 ;
int arr[] = { 1 , 2 , 3 , 5 };
int res = findDiff(arr, n);
if (res == - 1 )
System.out.print( "No prime numbers" ) ;
else
System.out.println( "Difference is " + res);
}
}
|
Python 3
MAX = 1000000
prime = [ True ] * ( MAX + 1 )
def SieveOfEratosthenes():
prime[ 1 ] = False
p = 2
c = 0
while (p * p < = MAX ) :
c + = 1
if (prime[p] = = True ) :
for i in range ( p * 2 , MAX + 1 , p):
prime[i] = False
p + = 1
def findDiff(arr, n):
min = MAX + 2
max = - 1
for i in range (n) :
if (prime[arr[i]] = = True ) :
if (arr[i] > max ):
max = arr[i]
if (arr[i] < min ):
min = arr[i]
return - 1 if ( max = = - 1 ) else ( max - min )
if __name__ = = "__main__" :
SieveOfEratosthenes()
n = 4
arr = [ 1 , 2 , 3 , 5 ]
res = findDiff(arr, n)
if (res = = - 1 ):
print ( "No prime numbers" )
else :
print ( "Difference is " ,res )
|
C#
using System;
class GFG
{
static int MAX = 1000000;
static bool []prime = new bool [MAX + 1];
static void SieveOfEratosthenes()
{
for ( int i = 0; i < MAX + 1; i++)
prime[i] = true ;
prime[1] = false ;
for ( int p = 2; p * p <= MAX; p++)
{
if (prime[p] == true )
{
for ( int i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
}
static int findDiff( int []arr, int n)
{
int min = MAX + 2, max = -1;
for ( int i = 0; i < n; i++)
{
if (prime[arr[i]] == true )
{
if (arr[i] > max)
max = arr[i];
if (arr[i] < min)
min = arr[i];
}
}
return (max == -1) ? -1 : (max - min);
}
public static void Main ()
{
SieveOfEratosthenes();
int n = 4;
int []arr = { 1, 2, 3, 5 };
int res = findDiff(arr, n);
if (res == -1)
Console.WriteLine( "No prime numbers" ) ;
else
Console.WriteLine( "Difference is " + res);
}
}
|
Javascript
<script>
MAX = 1000000;
prime = new Array(MAX + 1);
function SieveOfEratosthenes()
{
prime.fill( true );
prime[1] = false ;
for ( var p = 2; p * p <= MAX; p++)
{
if (prime[p] == true )
{
for ( var i = p * 2; i <= MAX; i += p)
prime[i] = false ;
}
}
}
function findDiff(arr, n)
{
var min = MAX + 2, max = -1;
for ( var i = 0; i < n; i++) {
if (prime[arr[i]] == true )
{
if (arr[i] > max)
max = arr[i];
if (arr[i] < min)
min = arr[i];
}
}
return (max == -1)? -1 : (max - min);
}
SieveOfEratosthenes();
var n = 4;
var arr = [ 1, 2, 3, 5 ];
var res = findDiff(arr, n);
if (res == -1)
document.write( "No prime numbers" + "<br>" );
else
document.write( "Difference is " + res + "<br>" );
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...