Count Sexy Prime Pairs in the given array
Given an array arr[] of size N containing natural numbers, the task is to count all possible pairs in the arr[] that are Sexy Prime Pairs.
A SPP (Sexy Prime Pair) are those numbers that are prime and having a difference 6 between the prime numbers. In other words, an SPP (Sexy Prime Pair) is a prime that has a prime gap of six.
Examples:
Input: arr[] = { 6, 7, 5, 11, 13 }
Output: 2
Explanation:
The 2 pairs are (5, 11) and (7, 13).
Input: arr[] = { 2, 4, 6, 11 }
Output: 0
Explanation:
There are no such pairs forming SPP (Sexy Prime Pair).
Naive Approach: To solve the problem mentioned above the idea is to find all possible pairs in the given array arr[] and check whether both the element in pairs are Prime Numbers and they differ by 6, then the current pairs form SPP (Sexy Prime Pair).
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 + 6) == 0) {
return false ;
}
}
return true ;
}
bool SexyPrime( int n1, int n2)
{
return (isPrime(n1)
&& isPrime(n2)
&& abs (n1 - n2) == 6);
}
int countSexyPairs( int arr[], int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (SexyPrime(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
int main()
{
int arr[] = { 6, 7, 5, 11, 13 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countSexyPairs(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 + 6 ) == 0 ) {
return false ;
}
}
return true ;
}
static boolean SexyPrime( int n1, int n2)
{
return (isPrime(n1)
&& isPrime(n2)
&& Math.abs(n1 - n2) == 6 );
}
static int countSexyPairs( int arr[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
if (SexyPrime(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 6 , 7 , 5 , 11 , 13 };
int n = arr.length;
System.out.print(
countSexyPairs(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 + 6 ) = = 0 ):
return False
return True
def SexyPrime(n1, n2):
return (isPrime(n1)
and isPrime(n2)
and abs (n1 - n2) = = 6 )
def countSexyPairs(arr, n):
count = 0
for i in range (n):
for j in range (i + 1 , n):
if (SexyPrime(arr[i], arr[j])):
count + = 1
return count
if __name__ = = '__main__' :
arr = [ 6 , 7 , 5 , 11 , 13 ]
n = len (arr)
print (countSexyPairs(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 + 6) == 0) {
return false ;
}
}
return true ;
}
static bool SexyPrime( int n1, int n2)
{
return (isPrime(n1)
&& isPrime(n2)
&& Math.Abs(n1 - n2) == 6);
}
static int countSexyPairs( int [] arr, int n)
{
int count = 0;
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (SexyPrime(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
public static void Main(String[] args)
{
int [] arr = { 6, 7, 5, 11, 13 };
int n = arr.Length;
Console.Write(countSexyPairs(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 + 6) == 0) {
return false ;
}
}
return true ;
}
function SexyPrime( n1, n2)
{
return (isPrime(n1)
&& isPrime(n2)
&& Math.abs(n1 - n2) == 6);
}
function countSexyPairs( arr, n)
{
var count = 0;
for ( var i = 0; i < n; i++) {
for ( var j = i + 1; j < n; j++) {
if (SexyPrime(arr[i], arr[j])) {
count++;
}
}
}
return count;
}
var arr = [ 6, 7, 5, 11, 13 ]
var n = arr.length;
document.write(countSexyPairs(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.
Efficient Approach:
The method mentioned above can be optimized by the following steps:
- Precompute all the Prime Numbers till maximum number in the given array arr[] using Sieve of Eratosthenes.
- Store all the frequency of all element for the given array and sort the array.
- For each element in the array, check if the element is prime or not.
- If the element is prime, then check if (element + 6) is a prime number or not and is present in the given array.
- If the (element + 6) is present, then the frequency of (element + 6) will give the count of pairs for the current element.
- Repeat the above step for all the element in the array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< bool > Prime;
void computePrime( int N)
{
Prime.resize(N + 1, true );
Prime[0] = Prime[1] = false ;
for ( int i = 2; i * i <= N; i++) {
if (Prime[i]) {
for ( int j = i * i; j < N; j += i) {
Prime[j] = false ;
}
}
}
}
int countSexyPairs( int arr[], int n)
{
int maxE = *max_element(arr, arr + n);
computePrime(maxE);
int count = 0;
int freq[maxE + 1] = { 0 };
for ( int i = 0; i < n; i++) {
freq[arr[i]]++;
}
sort(arr, arr + n);
for ( int i = 0; i < n; i++) {
if (Prime[arr[i]]) {
if (freq[arr[i] + 6] > 0
&& Prime[arr[i] + 6]) {
count++;
}
}
}
return count;
}
int main()
{
int arr[] = { 6, 7, 5, 11, 13 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countSexyPairs(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static boolean [] Prime;
static void computePrime( int N)
{
Prime = new boolean [N + 1 ];
Arrays.fill(Prime, true );
Prime[ 0 ] = Prime[ 1 ] = false ;
for ( int i = 2 ; i * i <= N; i++) {
if (Prime[i]) {
for ( int j = i * i; j < N; j += i) {
Prime[j] = false ;
}
}
}
}
static int countSexyPairs( int arr[], int n)
{
int maxE = Arrays.stream(arr)
.max()
.getAsInt();
computePrime(maxE);
int count = 0 ;
int freq[] = new int [maxE + 1 ];
for ( int i = 0 ; i < n; i++) {
freq[arr[i]]++;
}
Arrays.sort(arr);
for ( int i = 0 ; i < n; i++) {
if (Prime[arr[i]]) {
if (arr[i] + 6 < freq.length
&& freq[arr[i] + 6 ] > 0
&& Prime[arr[i] + 6 ]) {
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int arr[] = { 6 , 7 , 5 , 11 , 13 };
int n = arr.length;
System.out.print(
countSexyPairs(arr, n));
}
}
|
Python3
def computePrime( N):
Prime = [ True ] * (N + 1 )
Prime[ 0 ] = False
Prime[ 1 ] = False
i = 2
while i * i < = N:
if (Prime[i]):
for j in range ( i * i, N, i):
Prime[j] = False
i + = 1
return Prime
def countSexyPairs(arr, n):
maxE = max (arr)
Prime = computePrime(maxE)
count = 0
freq = [ 0 ] * (maxE + 6 )
for i in range ( n):
freq[arr[i]] + = 1
arr.sort()
for i in range (n):
if (Prime[arr[i]]):
if ((arr[i] + 6 ) < = (maxE)
and freq[arr[i] + 6 ] > 0
and Prime[arr[i] + 6 ]):
count + = 1
return count
if __name__ = = "__main__" :
arr = [ 6 , 7 , 5 , 11 , 13 ]
n = len (arr)
print ( countSexyPairs(arr, n))
|
C#
using System;
using System.Linq;
class GFG {
static bool [] Prime;
static void computePrime( int N)
{
Prime = new bool [N + 1];
for ( int i = 0; i <= N; i++) {
Prime[i] = true ;
}
Prime[0] = Prime[1] = false ;
for ( int i = 2; i * i <= N; i++) {
if (Prime[i]) {
for ( int j = i * i; j < N; j += i) {
Prime[j] = false ;
}
}
}
}
static int countSexyPairs( int [] arr, int n)
{
int maxE = arr.Max();
computePrime(maxE);
int count = 0;
int [] freq = new int [maxE + 1];
for ( int i = 0; i < n; i++) {
freq[arr[i]]++;
}
Array.Sort(arr);
for ( int i = 0; i < n; i++) {
if (Prime[arr[i]]) {
if (arr[i] + 6 < freq.Length
&& freq[arr[i] + 6] > 0
&& Prime[arr[i] + 6]) {
count++;
}
}
}
return count;
}
public static void Main(String[] args)
{
int [] arr = { 6, 7, 5, 11, 13 };
int n = arr.Length;
Console.Write(countSexyPairs(arr, n));
}
}
|
Javascript
<script>
var Prime = Array(100).fill( true );
function computePrime(N)
{
var i,j;
Prime[0] = Prime[1] = false ;
for (i = 2; i * i <= N; i++) {
if (Prime[i]) {
for (j = i * i; j < N; j += i) {
Prime[j] = false ;
}
}
}
}
function countSexyPairs(arr, n)
{
var maxE = Math.max.apply(Math, arr);
computePrime(maxE);
var count = 0;
var freq = Array(maxE + 1).fill(0);
for (i = 0; i < n; i++) {
freq[arr[i]]++;
}
arr.sort();
for (i = 0; i < n; i++) {
if (Prime[arr[i]]) {
if (freq[arr[i] + 6] > 0
&& Prime[arr[i] + 6]) {
count++;
}
}
}
return count;
}
var arr = [6, 7, 5, 11, 13];
var n = arr.length;
document.write(countSexyPairs(arr, n));
</script>
|
Time Complexity: O(N * sqrt(M)), where N is the number of elements in the given array, and M is the maximum element in the array.
Auxiliary Space Complexity: O(N)
Last Updated :
24 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...