Count pairs in an array such that at least one element is prime in C++

C++Server Side ProgrammingProgramming

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.

Approach used in the below program is as follows

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.

Example

 Live Demo

#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;
}

Output

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
raja
Published on 02-Dec-2020 16:06:50
Advertisements