
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the Number of Prime Pairs in an Array using C++
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.
- Related Articles
- Find the Number of Unique Pairs in an Array using C++
- Find number of pairs in an array such that their XOR is 0 using C++.
- Find the Pairs of Positive Negative values in an Array using C++\n
- Python Program to find out the number of matches in an array containing pairs of (base, number)
- Find the maximum cost of an array of pairs choosing at most K pairs in C++
- Find the frequency of a number in an array using C++.
- Product of every K’th prime number in an array in C++
- C++ program to find out the number of pairs in an array that satisfy a given condition
- Finding all possible prime pairs that sum upto input number using JavaScript
- Program to find array of doubled pairs using Python
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Count pairs in an array such that at least one element is prime in C++
- Find largest prime factor of a number using C++.
- Convert an array to reduced form (Using vector of pairs) in C++
- Count of pairs in an array that have consecutive numbers using JavaScript
