Given an array arr[] of distinct elements, the task is to count the total number of distinct pairs in which at least one element is prime.
Examples:
Input: arr[] = {1, 3, 10, 7, 8}
Output: 7
Pairs with at least one prime are (1, 3), (1, 7),
(3, 1), (3, 7), (3, 8), (10, 7), (7, 8).
Input:arr[]={4, 6, 8, 2, 9};
Output: 4
Approach: First precompute all the prime till the Maximum element of array using Sieve. Traverse each and every possible pair and check if any of the elements in the pair is prime. If yes, then increment the count.
Below is the implementation of above approach:
C++
// C++ implementation of the above approach
#include <bits/stdc++.h>
usingnamespacestd;
// Function to find primes
voidsieve(intmaxm, intprime[])
{
prime[0] = prime[1] = 1;
for(inti = 2; i * i <= maxm; i++)
if(!prime[i])
for(intj = 2 * i; j <= maxm; j += i)
prime[j] = 1;
}
// Function to count the pair
intcountPair(inta[], intn)
{
// Find the maximum element of the array
intmaxm = *max_element(a, a + n);
intprime[maxm + 1];
memset(prime, 0, sizeof(prime));
// Find primes upto maximum
sieve(maxm, prime);
// Count pairs with at least prime
intcount = 0;
for(inti = 0; i < n; i++)
for(intj = i + 1; j < n; j++)
if(prime[a[i]] == 0 || prime[a[j]] == 0)
count++;
returncount;
}
// Driver code
intmain()
{
intarr[] = { 2, 3, 5, 4, 7 };
intn = 5;
cout << countPair(arr, n);
return0;
}
Java
// Java implementation of the above approach
classGFG
{
// Function to find primes
staticvoidsieve(intmaxm, intprime[])
{
prime[0] = prime[1] = 1;
for(inti = 2; i * i <= maxm; i++)
if(prime[i] == 0)
for(intj = 2* i; j <= maxm; j += i)
prime[j] = 1;
}
// Function to count the pair
staticintcountPair(inta[], intn)
{
// Find the maximum element of the array
intmaxm = a[0];
for(inti = 1; i < n; i++)
if(a[i] > maxm)
maxm = a[i];
int[] prime = newint[maxm + 1];
for(inti = 0; i < maxm + 1; i++)
prime[i] = 0;
// Find primes upto maximum
sieve(maxm, prime);
// Count pairs with at least prime
intcount = 0;
for(inti = 0; i < n; i++)
for(intj = i + 1; j < n; j++)
if(prime[a[i]] == 0|| prime[a[j]] == 0)
count++;
returncount;
}
// Driver code
publicstaticvoidmain(String []args)
{
intarr[] = { 2, 3, 5, 4, 7};
intn = arr.length;
System.out.println(countPair(arr, n));
}
}
// This code is contributed by ihritik
Python3
# Python 3 implementation of the above approach
frommath importsqrt
# Function to count the pair
defcountPair(a, n):
# Find the maximum element of the array
maxm =a[0]
fori inrange(len(a)):
if(a[i] > maxm):
maxm =a[i]
prime =[0fori inrange(maxm +1)]
# Find primes upto maximum
prime[0] =prime[1] =1;
fori inrange(2, int(sqrt(maxm)) +1, 1):
if(prime[i] ==0):
forj inrange(2*i, maxm +1, i):
prime[j] =1
# Count pairs with at least prime
count =0
fori inrange(n):
forj inrange(i +1, n, 1):
if(prime[a[i]] ==0or
prime[a[j]] ==0):
count +=1
returncount
# Driver code
if__name__ =='__main__':
arr =[2, 3, 5, 4, 7]
n =5
print(countPair(arr, n))
# This code is contributed by
# Sanjit_Prasad
C#
// C# implementation of the above approach
usingSystem;
classGFG
{
// Function to find primes
staticvoidsieve(intmaxm, int[]prime)
{
prime[0] = prime[1] = 1;
for(inti = 2; i * i <= maxm; i++)
if(prime[i] == 0)
for(intj = 2 * i; j <= maxm; j += i)
prime[j] = 1;
}
// Function to count the pair
staticintcountPair(int[]a, intn)
{
// Find the maximum element of the array
intmaxm = a[0];
for(inti = 1; i < n; i++)
if(a[i] > maxm)
maxm = a[i];
int[] prime = newint[maxm + 1];
for(inti = 0; i < maxm + 1; i++)
prime[i] = 0;
// Find primes upto maximum
sieve(maxm, prime);
// Count pairs with at least prime
intcount = 0;
for(inti = 0; i < n; i++)
for(intj = i + 1; j < n; j++)
if(prime[a[i]] == 0 || prime[a[j]] == 0)
count++;
returncount;
}
// Driver code
publicstaticvoidMain()
{
int[]arr = { 2, 3, 5, 4, 7 };
intn = arr.Length;
Console.WriteLine(countPair(arr, n));
}
}
// This code is contributed by ihritik
PHP
<?php
// PHP implementation of the above approach
// Function to find primes
functionsieve($maxm, $prime)
{
$prime[0] = $prime[1] = 1;
for($i= 2; $i* $i<= $maxm; $i++)
if(!$prime[$i])
for($j= 2 * $i;
$j<= $maxm; $j+= $i)
$prime[$j] = 1;
}
// Function to count the pair
functioncountPair($a, $n)
{
// Find the maximum element of the array
$maxm= max($a);
$prime= array();
$prime= array_fill(0, $maxm+ 1, 0);
// Find primes upto maximum
sieve($maxm, $prime);
// Count pairs with at least prime
$count= 0;
for($i= 0; $i< $n; $i++)
for($j= $i+ 1; $j< $n; $j++)
if($prime[$a[$i]] == 0 ||
$prime[$a[$j]] == 0)
$count++;
return$count;
}
// Driver code
$arr= array( 2, 3, 5, 4, 7 );
$n= 5;
echocountPair($arr, $n);
// This code is contributed by Ryuga
?>
Javascript
<script>
// Javascript implementation of the above approach
// Function to find primes
functionsieve(maxm,prime)
{
prime[0] = prime[1] = 1;
for(let i = 2; i * i <= maxm; i++)
if(prime[i] == 0)
for(let j = 2 * i; j <= maxm; j += i)
prime[j] = 1;
}
// Function to count the pair
functioncountPair(a,n)
{
// Find the maximum element of the array
let maxm = a[0];
for(let i = 1; i < n; i++)
if(a[i] > maxm)
maxm = a[i];
let prime = newArray(maxm + 1);
for(let i = 0; i < maxm + 1; i++)
prime[i] = 0;
// Find primes upto maximum
sieve(maxm, prime);
// Count pairs with at least prime
let count = 0;
for(let i = 0; i < n; i++)
for(let j = i + 1; j < n; j++)
if(prime[a[i]] == 0 || prime[a[j]] == 0)
count++;
returncount;
}
// Driver code
let arr=[2, 3, 5, 4, 7];
let n = arr.length;
document.write(countPair(arr, n));
// This code is contributed by unknown2108
</script>
Output
10
Time Complexity: O(max(n2, m*log(log(m)))), where n is the size of the given array and m is the maximum element in the array. Auxiliary Space: O(m), where m is the maximum element in the array.
Efficient Approach:
Approach: First precompute all the prime till the Maximum element of array using Sieve. Maintain the count of primes and non-primes. Then count pairs with single prime i.e. nonPrimes * Primes and count pairs with both primes (Primes * (Primes – 1)) / 2. Return the sum of both counts.
// JavaScript implementation of the above approach
// Function to find primes
functionsieve(maxm, prime)
{
prime[0] = prime[1] = 1;
for(let i = 2; i * i <= maxm; i++)
if(prime[i] == 0)
for(let j = 2 * i; j <= maxm; j += i)
prime[j] = 1;
}
functioncountPair(a, n)
{
// Find the maximum element of the array
let maxm = a[0];
let i;
for( i = 1; i < n ;i++)
if(a[i] > maxm)
maxm = a[i];
let prime = newArray(maxm + 1);
for( i = 0; i < maxm + 1 ;i++)
prime[i] = 0;
// Find primes upto maximum
sieve(maxm, prime);
// Count number of primes
let countPrimes = 0;
for( i = 0; i < n; i++)
if(prime[a[i]] == 0)
countPrimes++;
let nonPrimes = n - countPrimes;
let pairswith1Prime = nonPrimes * countPrimes;
let pairsWith2Primes = parseInt((countPrimes *
(countPrimes - 1)) / 2, 10);
return(pairswith1Prime + pairsWith2Primes);
}
let arr = [ 2, 3, 5, 4, 7 ];
let n = arr.length;
document.write(countPair(arr, n));
</script>
Output
10
Time Complexity: O(max(n, m*log(log(m)))), where n is the size of the given array and m is the maximum element in the array. Auxiliary Space: O(m), where m is the maximum element in the array.
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 neveropen!