Count pairs in an array such that at least one element is prime
Last Updated :
29 Nov, 2022
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>
using namespace std;
// Function to find primes
void sieve(int maxm, int prime[])
{
prime[0] = prime[1] = 1;
for (int i = 2; i * i <= maxm; i++)
if (!prime[i])
for (int j = 2 * i; j <= maxm; j += i)
prime[j] = 1;
}
// Function to count the pair
int countPair(int a[], int n)
{
// Find the maximum element of the array
int maxm = *max_element(a, a + n);
int prime[maxm + 1];
memset(prime, 0, sizeof(prime));
// Find primes upto maximum
sieve(maxm, prime);
// Count pairs with at least prime
int count = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (prime[a[i]] == 0 || prime[a[j]] == 0)
count++;
return count;
}
// Driver code
int main()
{
int arr[] = { 2, 3, 5, 4, 7 };
int n = 5;
cout << countPair(arr, n);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to find primes
static void sieve(int maxm, int prime[])
{
prime[0] = prime[1] = 1;
for (int i = 2; i * i <= maxm; i++)
if (prime[i] == 0)
for (int j = 2 * i; j <= maxm; j += i)
prime[j] = 1;
}
// Function to count the pair
static int countPair(int a[], int n)
{
// Find the maximum element of the array
int maxm = a[0];
for(int i = 1; i < n; i++)
if(a[i] > maxm)
maxm = a[i];
int [] prime = new int[maxm + 1];
for(int i = 0; i < maxm + 1; i++)
prime[i] = 0;
// Find primes upto maximum
sieve(maxm, prime);
// Count pairs with at least prime
int count = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (prime[a[i]] == 0 || prime[a[j]] == 0)
count++;
return count;
}
// Driver code
public static void main(String []args)
{
int arr[] = { 2, 3, 5, 4, 7 };
int n = arr.length;
System.out.println(countPair(arr, n));
}
}
// This code is contributed by ihritik
Python3
# Python 3 implementation of the above approach
from math import sqrt
# Function to count the pair
def countPair(a, n):
# Find the maximum element of the array
maxm = a[0]
for i in range(len(a)):
if(a[i] > maxm):
maxm = a[i]
prime = [0 for i in range(maxm + 1)]
# Find primes upto maximum
prime[0] = prime[1] = 1;
for i in range(2, int(sqrt(maxm)) + 1, 1):
if (prime[i] == 0):
for j in range(2 * i, maxm + 1, i):
prime[j] = 1
# Count pairs with at least prime
count = 0
for i in range(n):
for j in range(i + 1, n, 1):
if (prime[a[i]] == 0 or
prime[a[j]] == 0):
count += 1
return count
# 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
using System;
class GFG
{
// Function to find primes
static void sieve(int maxm, int []prime)
{
prime[0] = prime[1] = 1;
for (int i = 2; i * i <= maxm; i++)
if (prime[i] == 0)
for (int j = 2 * i; j <= maxm; j += i)
prime[j] = 1;
}
// Function to count the pair
static int countPair(int []a, int n)
{
// Find the maximum element of the array
int maxm = a[0];
for(int i = 1; i < n; i++)
if(a[i] > maxm)
maxm = a[i];
int [] prime = new int[maxm + 1];
for(int i = 0; i < maxm + 1; i++)
prime[i] = 0;
// Find primes upto maximum
sieve(maxm, prime);
// Count pairs with at least prime
int count = 0;
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++)
if (prime[a[i]] == 0 || prime[a[j]] == 0)
count++;
return count;
}
// Driver code
public static void Main()
{
int []arr = { 2, 3, 5, 4, 7 };
int n = 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
function sieve($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
function countPair($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;
echo countPair($arr, $n);
// This code is contributed by Ryuga
?>
JavaScript
<script>
// Javascript implementation of the above approach
// Function to find primes
function sieve(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
function countPair(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 = new Array(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++;
return count;
}
// 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>
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.
Below is the implementation of above approach:
C++
// C++ implementation of the above approach
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
// Function to find primes
void sieve(int maxm, int prime[])
{
prime[0] = prime[1] = 1;
for (int i = 2; i * i <= maxm; i++)
if (!prime[i])
for (int j = 2 * i; j <= maxm; j += i)
prime[j] = 1;
}
ll countPair(int a[], int n)
{
// Find the maximum element of the array
int maxm = *max_element(a, a + n);
int prime[maxm + 1];
memset(prime, 0, sizeof(prime));
// Find primes upto maximum
sieve(maxm, prime);
// Count number of primes
int countPrimes = 0;
for (int i = 0; i < n; i++)
if (prime[a[i]] == 0)
countPrimes++;
int nonPrimes = n - countPrimes;
ll pairswith1Prime = nonPrimes * countPrimes;
ll pairsWith2Primes = (countPrimes * (countPrimes - 1)) / 2;
return pairswith1Prime + pairsWith2Primes;
}
// Driver code
int main()
{
int arr[] = { 2, 3, 5, 4, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << countPair(arr, n);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to find primes
static void sieve(int maxm, int []prime)
{
prime[0] = prime[1] = 1;
for (int i = 2; i * i <= maxm; i++)
if (prime[i]==0)
for (int j = 2 * i; j <= maxm; j += i)
prime[j] = 1;
}
static long countPair(int []a, int n)
{
// Find the maximum element of the array
int maxm = a[0];
int i;
for( i = 1; i < n ; i++)
if(a[i] > maxm)
maxm = a[i];
int [] prime = new int[maxm + 1];
for( i = 0; i < maxm + 1 ;i++)
prime[i] = 0;
// Find primes upto maximum
sieve(maxm, prime);
// Count number of primes
int countPrimes = 0;
for ( i = 0; i < n; i++)
if (prime[a[i]] == 0)
countPrimes++;
int nonPrimes = n - countPrimes;
long pairswith1Prime = nonPrimes *
countPrimes;
long pairsWith2Primes = (countPrimes *
(countPrimes - 1)) / 2;
return pairswith1Prime + pairsWith2Primes;
}
// Driver code
public static void main(String []args)
{
int [] arr = { 2, 3, 5, 4, 7 };
int n = arr.length;
System.out.println(countPair(arr, n));
}
}
// This code is contributed by ihritik
Python3
# Python3 implementation of the above approach
# Function to find primes
def sieve(maxm, prime):
prime[0] = prime[1] = 1;
i = 2;
while (i * i <= maxm):
if (prime[i] == 0):
for j in range(2 * i, maxm + 1, i):
prime[j] = 1;
i += 1;
def countPair(a, n):
# Find the maximum element
# of the array
maxm = max(a);
prime = [0] * (maxm + 1);
# Find primes upto maximum
sieve(maxm, prime);
# Count number of primes
countPrimes = 0;
for i in range(n):
if (prime[a[i]] == 0):
countPrimes += 1;
nonPrimes = n - countPrimes;
pairswith1Prime = nonPrimes * countPrimes;
pairsWith2Primes = (countPrimes *
(countPrimes - 1)) // 2;
return pairswith1Prime + pairsWith2Primes;
# Driver code
arr = [ 2, 3, 5, 4, 7 ];
n = len(arr);
print(countPair(arr, n));
# This code is contributed by mits
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to find primes
static void sieve(int maxm, int []prime)
{
prime[0] = prime[1] = 1;
for (int i = 2; i * i <= maxm; i++)
if (prime[i] == 0)
for (int j = 2 * i; j <= maxm; j += i)
prime[j] = 1;
}
static long countPair(int []a, int n)
{
// Find the maximum element of the array
int maxm = a[0];
int i;
for( i = 1; i < n ;i++)
if(a[i] > maxm)
maxm = a[i];
int [] prime = new int[maxm + 1];
for( i = 0; i < maxm + 1 ;i++)
prime[i] = 0;
// Find primes upto maximum
sieve(maxm, prime);
// Count number of primes
int countPrimes = 0;
for ( i = 0; i < n; i++)
if (prime[a[i]] == 0)
countPrimes++;
int nonPrimes = n - countPrimes;
long pairswith1Prime = nonPrimes *
countPrimes;
long pairsWith2Primes = (countPrimes *
(countPrimes - 1)) / 2;
return pairswith1Prime + pairsWith2Primes;
}
// Driver code
public static void Main()
{
int [] arr = { 2, 3, 5, 4, 7 };
int n = 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
function sieve($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 countPair($a, $n)
{
// Find the maximum element
// of the array
$maxm = max($a);
$prime = array_fill(0, $maxm + 1,0);
// Find primes upto maximum
sieve($maxm, $prime);
// Count number of primes
$countPrimes = 0;
for ($i = 0; $i < $n; $i++)
if ($prime[$a[$i]] == 0)
$countPrimes++;
$nonPrimes = $n - $countPrimes;
$pairswith1Prime = $nonPrimes * $countPrimes;
$pairsWith2Primes = ($countPrimes *
($countPrimes - 1)) / 2;
return $pairswith1Prime + $pairsWith2Primes;
}
// Driver code
$arr = array( 2, 3, 5, 4, 7 );
$n = count($arr);
echo countPair($arr, $n);
// This code is contributed by mits
?>
JavaScript
<script>
// JavaScript implementation of the above approach
// Function to find primes
function sieve(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 countPair(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 = new Array(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>
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.
Similar Reads
Count of pairs in an Array whose sum is Prime
Given an array arr of size N elements, the task is to count the number of pairs of elements in the array whose sum is prime. Examples: Input: arr = {1, 2, 3, 4, 5} Output: 5 Explanation: Pairs with sum as a prime number are: {1, 2}, {1, 4}, {2, 3}, {2, 5} and {3, 4} Input: arr = {10, 20, 30, 40} Out
10 min read
Count of elements A[i] such that A[i] + 1 is also present in the Array
Given an integer array arr the task is to count the number of elements 'A[i]', such that A[i] + 1 is also present in the array.Note: If there are duplicates in the array, count them separately.Examples: Input: arr = [1, 2, 3] Output: 2 Explanation: 1 and 2 are counted cause 2 and 3 are in arr.Input:
11 min read
Find the array element having equal count of Prime Numbers on its left and right
Given an array arr[] consisting of N positive integers, the task is to find an index from the array having count of prime numbers present on its left and right are equal. Examples: Input: arr[] = {2, 3, 4, 7, 5, 10, 1, 8}Output: 2Explanation: Consider the index 2, then the prime numbers to its left
13 min read
Count pairs with sum as a prime number and less than n
Given a positive integer n, count distinct number of pairs (x, y) that satisfy following conditions : (x + y) is a prime number.(x + y) < nx != y1 <= x, y Examples: Input : n = 6 Output : 3 prime pairs whose sum is less than 6 are: (1,2), (1,4), (2,3) Input : 12 Output : 11 prime pairs whose s
10 min read
Count array elements having at least one smaller element on its left and right side
Given an array arr[] of length N, the task is to find the number of elements in array arr[] which contains at least one smaller element on its left and right. Examples: Input: arr[] = {3, 9, 4, 6, 7, 5}Output: 3Explanation: Following 3 array elements satisfy the necessary conditions: arr[1] (= 9) ha
6 min read
Count of pairs in given Array whose GCD is not prime
Given an array arr[] consisting of N positive integers, the task is to find the number of pairs such that the Greatest Common Divisor(GCD) of the pairs is not a prime number. The pair (i, j) and (j, i) are considered the same. Examples: Input: arr[] ={ 2, 3, 9}Output: 10Explanation:Following are the
9 min read
Check for an array element that is co-prime with all others
Given an array arr[] of positive integers where 2 ? arr[i] ? 106 for all possible values of i. The task is to check whether there exists at least one element in the given array that forms co-prime pair with all other elements of the array. If no such element exists then print No else print Yes. Exam
14 min read
Print array elements that are divisible by at-least one other
Given an array of length N that contains only integers, the task is to print the special numbers of array. A number in this array is called Special number if it is divisible by at least one other number in the array. Examples : Input : 1 2 3 Output : 2 3 Explanation : both 2 and 3 are divisible by 1
9 min read
Count of non co-prime pairs from the range [1, arr[i]] for every array element
Given an array arr[] consisting of N integers, the task for every ith element of the array is to find the number of non co-prime pairs from the range [1, arr[i]]. Examples: Input: N = 2, arr[] = {3, 4}Output: 2 4Explanation: All non-co-prime pairs from the range [1, 3] are (2, 2) and (3, 3).All non-
13 min read
Modify array by merging elements with addition such that it consists of only Primes.
Given an array arr[] consisting of positive integers, the task is to check whether we can modify the array by adding any of the elements of the array such that it consists of only Primes. Examples: Input: arr[] = {3, 5, 7, 21} Output: YES Explanation: Add the following elements, 3+5+21 So the array
10 min read