- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given an array of positive integers. The goal is to find the count of distinct pairs of elements of an array that have at-least one prime member. If the array is [1,2,3,4] then pairs would be (1,2), (1,3), (2,3), (2,4) and (3,4).

Let us understand with examples

**Input** − arr[] = { 1,2,4,8,10 };

**Output** − Count of pairs in an array such that at least one element is prime are − 4

**Explanation** − The only prime element is 2 and pairing it with all others will give − (1,2), (2,4), (2,8), (2,10).

**Input** − arr[] = { 0,1,4,6,15 };

**Output** − Count of pairs in an array such that at least one element is prime are − 0

**Explanation** − The array has no prime element.

We will create an array arr_2[] for marking primes and non-primes. If arr_2[i] is 0 then i is prime else non-prime. If for any pair any value arr_2[A], arr_2[B] is 0 then pair (A,B) is counted.

Take an array arr[] of positive integers.

Function check_prime(int temp, int arr_2[] takes a value temp as highest and an array arr_2[] and populates arr_2[] with 0 for index as prime else 1.

Set arr_2[0]=arr_2[1]=0 as both 0 and 1 are non-prime.

Now using for loop, traverse from i=2 to i*i<temp.

Traverse from j=2*i to j<=temp and j+=i. Set arr_2[j]=1 for non primes.

Function Prime_Pairs(int arr[], int size) takes an array and its size and returns the count of such pairs in which at-least one element is prime.

Take the initial count as 0.

Initialize temp=*max_element(arr, arr + size) as maximum value among arrays.

Call check_prime(temp,arr_2). Where arr_2[] is initialized with 0’s and has length temp.

Now we will have arr_2[] where arr[i] is 0 for i as prime and 1 for i as non-prime.

Traverse array using two for loops from i=0 to i<size and j=0 to j<size.

For each pair arr[i], arr[j] check if arr_2[ arr[i] ] == 0 or arr_2[ arr[j] ] == 0. If yes then increment count.

Return count at the end of all loops as result.

#include <bits/stdc++.h> using namespace std; void check_prime(int temp, int arr_2[]){ arr_2[0] = 1; arr_2[1] = 1; for(int i = 2; i * i <= temp; i++){ if (arr_2[i]==0){ for (int j = 2 * i; j <= temp; j += i){ arr_2[j] = 1; } } } } int Prime_Pairs(int arr[], int size){ int count = 0; int temp = *max_element(arr, arr + size); int arr_2[temp + 1]; memset(arr_2, 0, sizeof(arr_2)); check_prime(temp, arr_2); for (int i = 0; i < size; i++){ for (int j = i + 1; j < size; j++){ if (arr_2[arr[i]] == 0 || arr_2[arr[j]] == 0){ count++; } } } return count; } int main(){ int arr[] = { 3, 5, 2, 7, 11, 14 }; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of pairs in an array such that at least one element is prime are: "<<Prime_Pairs(arr, size); return 0; }

If we run the above code it will generate the following output −

Count of pairs in an array such that at least one element is prime are: 15

- Related Questions & Answers
- Count pairs in an array such that frequency of one is at least value of other in C++
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times in C++
- Count elements that are divisible by at-least one element in another array in C++
- Count of pairs (x, y) in an array such that x < y in C++
- Count pairs in an array such that both elements has equal set bits in C++
- Print array elements that are divisible by at-least one other in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Find number of pairs in an array such that their XOR is 0 using C++.
- Find prime number K in an array such that (A[i] % K) is maximum in C++
- Count pairs of parentheses sequences such that parentheses are balanced in C++
- Count divisors of n that have at-least one digit common with n in Java
- Count divisible pairs in an array in C++
- Find all pairs (a, b) in an array such that a % b = k in C++
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j]) in C++
- Count of sub-strings that contain character X at least once in C++

Advertisements