Count twin prime pairs in an Array
Last Updated :
11 Oct, 2022
Given an array arr[] of N natural numbers. The task is to count all possible pairs in the arr[] that are Twin Primes.
A Twin prime are those numbers that are prime and have a difference of two ( 2 ) between the two prime numbers. In other words, a twin prime is a prime that has a prime gap of two.
Examples:
Input: arr[] = { 2, 3, 5, 7}
Output: 2
Explanation:
The 2 pairs are (3, 5) and (5, 7).
Input: arr[] = { 2, 4, 6, 11}
Output: 0
Explanation:
There are no such pairs forming Twin Primes.
Naive Approach:
The idea is to find all possible pairs in the given array arr[] and check whether both the elements in pairs are Prime Numbers and they differ by 2, then the current pairs form Twin Primes.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false ;
}
}
return true ;
}
bool twinPrime( int n1, int n2)
{
return (isPrime(n1)
&& isPrime(n2)
&& abs (n1 - n2) == 2);
}
int countTwinPairs( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (twinPrime(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
int main()
{
int arr[] = { 2, 3, 5, 11 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countTwinPairs(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
if (n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ; i * i <= n; i += 6 ) {
if (n % i == 0 || n % (i + 2 ) == 0 ) {
return false ;
}
}
return true ;
}
static boolean twinPrime( int n1, int n2)
{
return (isPrime(n1)
&& isPrime(n2)
&& Math.abs(n1 - n2) == 2 );
}
static int countTwinPairs( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
if (twinPrime(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 5 , 11 };
int n = arr.length;
System.out.print(countTwinPairs(arr, n));
}
}
|
Python 3
from math import sqrt
def isPrime(n):
if (n < = 1 ):
return False
if (n < = 3 ):
return True
if (n % 2 = = 0 or n % 3 = = 0 ):
return False
for i in range ( 5 , int (sqrt(n)) + 1 , 6 ):
if (n % i = = 0 or n % (i + 2 ) = = 0 ):
return False
return True
def twinPrime(n1, n2):
return (isPrime(n1) and isPrime(n2) and abs (n1 - n2) = = 2 )
def countTwinPairs(arr, n):
count = 0
for i in range (n):
for j in range (i + 1 ,n):
if (twinPrime(arr[i], arr[j])):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 2 , 3 , 5 , 11 ]
n = len (arr)
print (countTwinPairs(arr, n))
|
C#
using System;
class GFG{
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false ;
}
}
return true ;
}
static bool twinPrime( int n1, int n2)
{
return (isPrime(n1)
&& isPrime(n2)
&& Math.Abs(n1 - n2) == 2);
}
static int countTwinPairs( int []arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (twinPrime(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 5, 11 };
int n = arr.Length;
Console.Write(countTwinPairs(arr, n));
}
}
|
Javascript
<script>
function isPrime(n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( var i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false ;
}
}
return true ;
}
function twinPrime(n1, n2)
{
return (isPrime(n1)
&& isPrime(n2)
&& Math.abs(n1 - n2) == 2);
}
function countTwinPairs(arr, n)
{
var count = 0;
for ( var i = 0; i < n; i++) {
for ( var j = i + 1; j < n; j++) {
if (twinPrime(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
var arr=[ 2, 3, 5, 11 ];
var n = arr.length;
document.write(countTwinPairs(arr, n));
</script>
|
Time Complexity: O(sqrt(M)*N2), where N is the number of elements in the given array and M is the maximum element in the array
Auxiliary Space: O(1)
Efficient Approach:
- Precompute all the Prime Numbers till maximum number in the given array arr[] using Sieve of Eratosthenes.
- Store all the frequencies of all elements for the given array arr[].
- Sort the given array arr[].
- For each element in the array, check if the element is prime or not.
- If the element is, prime number then check if (element+2) is a prime number and is present in the given array arr[].
- If the (element+2) is present, then the frequency of (element+2) will give the count of pairs for the current element.
- Repeat the above step for all the elements in the array.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...