Find the Number of Prime Pairs in an Array using C++

C++Server Side ProgrammingProgramming

In this article, we will explain everything about finding the number of prime pairs in an array using C++. We have an array arr[] of integers, and we need to find all the possible prime pairs present in it. So here is the example for the problem −

Input : arr[ ] = { 1, 2, 3, 5, 7, 9 }

Output : 6

From the given array, prime pairs are
(2, 3), (2, 5), (2, 7), (3, 5), (3, 7), (5, 7)

Input : arr[] = {1, 4, 5, 9, 11}

Output : 1

Approaches to Find the Solution

Brute Force Approach

Now we will discuss the most basic approach of all, i.e., the Brute Force approach, and try to find another approach as this approach is not very efficient.

Example

#include <bits/stdc++.h>
using namespace std;
void seiveOfEratosthenes(int *arr, bool *prime, int n, int MAX){
    bool p[MAX+1];
    memset(p, true, sizeof(p));
    p[1] = false;
    p[0] = false;
     for(int i = 2; i * i <= MAX; i++){
        if(p[i] == true){
            for(int j = i*2; j <= MAX; j += i){
               p[j] = false;
            }
        }
    }
    for(int i = 0; i < n; i++){
        if(p[arr[i]] == true)
           prime[i] = true;
    }
}
int main(){
    int arr[] = {1, 2, 3, 5, 7, 8, 9};
    int n = sizeof(arr) / sizeof(arr[0]); // size of our array.
    int answer = 0; // counter variable to count the number of prime pairs.
    int MAX = INT_MIN; // Max element
    for(int i = 0; i < n; i++){
       MAX = max(MAX, arr[i]);
    }
    bool prime[n]; // boolean array that tells if the element is prime or not.
    memset(prime, false, sizeof(prime)); // initializing all the elements with value of false.
    seiveOfEratosthenes(arr, prime, n, MAX);
    for(int i = 0; i < n-1; i++){
        for(int j = i+1; j < n; j++){
            if(prime[i] == true && prime[j] == true)
               answer++;
         }
    }
    cout << answer << "\n";
    return 0;
}

Output

6

In this approach, we are making a bool array that is going to tell us if any element is prime or not, and then we are going through all the possible pairs and checking if both numbers in the pair are prime or not. If prime, then increment the answer by one and go on.

But this approach is not very efficient as its time complexity is O(N*N), where N is the size of our array so, now we are going to make this approach faster.

Efficient Approach

In this approach, most of the code will be the same, but the crucial change is that instead of going through all the possible pairs, we are just going to calculate them using a formula.

Example

#include <bits/stdc++.h>
using namespace std;
void seiveOfEratosthenes(int *arr, bool *prime, int n, int MAX){
   bool p[MAX+1];
   memset(p, true, sizeof(p));
   p[1] = false;
   p[0] = false;
   for(int i = 2; i * i <= MAX; i++){
       if(p[i] == true){
           for(int j = i*2; j <= MAX; j += i){
               p[j] = false;
           }
       }
    }
    for(int i = 0; i < n; i++){
       if(p[arr[i]] == true)
           prime[i] = true;
   }
}
int main(){
   int arr[] = {1, 2, 3, 5, 7, 8, 9};
   int n = sizeof(arr) / sizeof(arr[0]); // size of our array.
   int answer = 0; // counter variable to count the number of prime pairs.
   int MAX = INT_MIN; // Max element
   for(int i = 0; i < n; i++){
       MAX = max(MAX, arr[i]);
   }
   bool prime[n]; // boolean array that tells if the element is prime or not.
   memset(prime, false, sizeof(prime)); // initializing all the elements with value of false.
   seiveOfEratosthenes(arr, prime, n, MAX);
   for(int i = 0; i < n; i++){
       if(prime[i] == true)
           answer++;
   }
   answer = (answer * (answer - 1)) / 2;
   cout << answer << "\n";
   return 0;
}

Output

6

As you can see, most of the code is the same as the previous approach, but the crucial change that drastically decreased our complexity is the formula that we used, i.e., n(n-1)/2, which will calculate our number of prime pairs.

Explanation of the Above Code

In this code, we are using Sieve of Eratosthenes to mark all the prime numbers until the Max element that we have in the array. In another bool array, we are index-wise marking the elements if they are prime or not.

Finally, we are traversing through the whole array, finding the total number of primes present, and finding all the possible pairs using formula n*(n-1)/2. With this formula, our complexity is reduced to O(N), where N is the size of our array.

Conclusion

In this article, we solve a problem to find the Number of prime pairs present in an array in O(n) time complexity. We also learned the C++ program for this problem and the complete approach (Normal and efficient) by which we solved this problem. We can write the same program in other languages such as C, java, python, and other languages.

raja
Updated on 24-Nov-2021 11:03:12

Advertisements