Count pairs in an array whose product is composite number
Last Updated :
18 May, 2021
Given an array, arr[] of size N, the task is to count all the pairs of the given array whose product is a composite number.
Examples:
Input: arr[] = {1, 4, 7}
Output: 2
Explanation:
Pairs whose product is a composite number are:(4, 7), (1, 4).
Therefore, the required output is 2.
Input: arr[] = {1, 2, 8, 10}
Output: 5
Naive approach: The idea is to traverse the array and generate all the possible pairs of the given array. For each pair, check if the product of its elements is a composite number or not. If found to be true, then increment the count by 1. Follow the steps below to solve the problem:
- Initialize a variable, say res to store the count of pairs whose product is a composite number.
- Traverse the array and generate all possible pairs of the given array.
- For each pair, check if their product is composite or not. If found to be true, then increment the value of res by 1.
- Finally, print the value of res.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
bool isComposite( int N)
{
for ( int i = 2; i * i <= N;
i++) {
if (N % i == 0) {
return true ;
}
}
return false ;
}
int compositePair( int arr[], int N)
{
int res = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N;
j++) {
int prod = arr[i] * arr[j];
if (isComposite(prod)) {
res++;
}
}
}
return res;
}
int main()
{
int arr[] = { 1, 1, 2, 2, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << compositePair(arr, N);
return 0;
}
|
Java
import java.io.*;
class GFG{
static boolean isComposite( int N)
{
for ( int i = 2 ; i * i <= N; i++)
{
if (N % i == 0 )
{
return true ;
}
}
return false ;
}
static int compositePair( int arr[],
int N)
{
int res = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = i + 1 ; j < N; j++)
{
int prod = arr[i] * arr[j];
if (isComposite(prod))
{
res++;
}
}
}
return res;
}
public static void main (String[] args)
{
int arr[] = { 1 , 1 , 2 , 2 , 8 };
int N = arr.length;
System.out.println(compositePair(arr, N));
}
}
|
Python3
def isComposite(N):
for i in range ( 2 , N + 1 ):
if i * i > N:
break
if (N % i = = 0 ):
return True
return False
def compositePair(arr, N):
res = 0
for i in range (N):
for j in range (i + 1 , N):
prod = arr[i] * arr[j]
if (isComposite(prod)):
res + = 1
return res
if __name__ = = '__main__' :
arr = [ 1 , 1 , 2 , 2 , 8 ]
N = len (arr)
print (compositePair(arr, N))
|
C#
using System;
class GFG{
static bool isComposite( int N)
{
for ( int i = 2; i * i <= N; i++)
{
if (N % i == 0)
{
return true ;
}
}
return false ;
}
static int compositePair( int []arr,
int N)
{
int res = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
int prod = arr[i] * arr[j];
if (isComposite(prod))
{
res++;
}
}
}
return res;
}
public static void Main(String[] args)
{
int []arr = {1, 1, 2, 2, 8};
int N = arr.Length;
Console.WriteLine(compositePair(arr, N));
}
}
|
Javascript
<script>
function isComposite(N)
{
var i;
for (i = 2; i * i <= N; i++) {
if (N % i == 0) {
return true ;
}
}
return false ;
}
function compositePair(arr, N)
{
var res = 0;
var i,j;
for (i = 0; i < N; i++) {
for (j = i + 1; j < N;
j++) {
var prod = arr[i] * arr[j];
if (isComposite(prod)) {
res++;
}
}
}
return res;
}
var arr = [1, 1, 2, 2, 8];
var N = arr.length;
document.write(compositePair(arr, N));
</script>
|
Time Complexity: O(N2 √X), where X is the maximum possible product of a pair in the given array.
Auxiliary Space: O(1)
Efficient Approach: TO optimize the above approach, the idea is to use the fact that all the prime numbers and 1s are not composite numbers. Follow the steps below to solve the problem:
- Initialize two variables cntPrime and cntOne to store the count of 1s and prime numbers in the given array respectively.
- Initialize a variable, say res to store the count of pairs whose product is a composite number.
- Total pairs whose product is not a composite number is:
cntNonComp = cntPrime × cntOne + cntOne × (cntOne – 1) / 2.
- Therefore, the total count of pairs whose product is a composite number is:
res = N × (N – 1) / 2 – cntNonComp.
- Finally, print the value of res.
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
#define X 1000000
vector< bool > getPrimeNum()
{
vector< bool > isPrime(X, true );
isPrime[0] = false ;
isPrime[1] = false ;
for ( int i = 2; i * i <= X;
i++) {
if (isPrime[i] == true ) {
for ( int j = i * i;
j < X; j += i) {
isPrime[j] = false ;
}
}
}
return isPrime;
}
int cntPairs( int arr[], int N)
{
vector< bool > isPrime
= getPrimeNum();
int cntOne = 0;
int cntPrime = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] == 1) {
cntOne += 1;
}
else if (isPrime[i]) {
cntPrime += 1;
}
}
int cntNonComp = 0;
cntNonComp = cntPrime * cntOne
+ cntOne * (cntOne - 1) / 2;
int res = 0;
res = N * (N - 1) / 2 - cntNonComp;
return res;
}
int main()
{
int arr[] = { 1, 1, 2, 2, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << cntPairs(arr, N);
}
|
Java
import java.util.*;
class GFG{
public static int X = 1000000 ;
public static boolean [] getPrimeNum()
{
boolean isPrime[] = new boolean [X];
Arrays.fill(isPrime, true );
isPrime[ 0 ] = false ;
isPrime[ 1 ] = false ;
for ( int i = 2 ; i * i <= X; i++)
{
if (isPrime[i] == true )
{
for ( int j = i * i; j < X; j += i)
{
isPrime[j] = false ;
}
}
}
return isPrime;
}
public static int cntPairs( int arr[], int N)
{
boolean isPrime[] = getPrimeNum();
int cntOne = 0 ;
int cntPrime = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == 1 )
{
cntOne += 1 ;
}
else if (isPrime[i])
{
cntPrime += 1 ;
}
}
int cntNonComp = 0 ;
cntNonComp = cntPrime * cntOne +
cntOne * (cntOne - 1 ) / 2 ;
int res = 0 ;
res = N * (N - 1 ) / 2 - cntNonComp;
return res;
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , 2 , 2 , 8 };
int N = arr.length;
System.out.println(cntPairs(arr, N));
}
}
|
Python3
X = 1000000
def getPrimeNum():
isPrime = [ True ] * (X)
isPrime[ 0 ] = False
isPrime[ 1 ] = False
i = 2
while i * i < = X:
if (isPrime[i] = = True ):
for j in range (i * i,
X, i):
isPrime[j] = False
i + = 1
return isPrime
def cntPairs(arr, N):
isPrime = getPrimeNum()
cntOne = 0
cntPrime = 0
for i in range (N):
if (arr[i] = = 1 ):
cntOne + = 1
elif (isPrime[i]):
cntPrime + = 1
cntNonComp = 0
cntNonComp = (cntPrime * cntOne +
cntOne * (cntOne - 1 ) / / 2 )
res = 0
res = (N * (N - 1 ) / / 2 -
cntNonComp)
return res
if __name__ = = "__main__" :
arr = [ 1 , 1 , 2 , 2 , 8 ]
N = len (arr)
print (cntPairs(arr, N))
|
C#
using System;
class GFG{
public static int X = 1000000;
public static bool [] getPrimeNum()
{
bool []isPrime = new bool [X];
for ( int i = 0; i < X; i++)
isPrime[i] = true ;
isPrime[0] = false ;
isPrime[1] = false ;
for ( int i = 2;
i * i <= X; i++)
{
if (isPrime[i] == true )
{
for ( int j = i * i;
j < X; j += i)
{
isPrime[j] = false ;
}
}
}
return isPrime;
}
public static int cntPairs( int []arr,
int N)
{
bool []isPrime = getPrimeNum();
int cntOne = 0;
int cntPrime = 0;
for ( int i = 0; i < N; i++)
{
if (arr[i] == 1)
{
cntOne += 1;
}
else if (isPrime[i])
{
cntPrime += 1;
}
}
int cntNonComp = 0;
cntNonComp = cntPrime * cntOne +
cntOne * (cntOne - 1) / 2;
int res = 0;
res = N * (N - 1) / 2 - cntNonComp;
return res;
}
public static void Main(String[] args)
{
int []arr = {1, 1, 2, 2, 8};
int N = arr.Length;
Console.WriteLine(cntPairs(arr, N));
}
}
|
Javascript
<script>
let X = 1000000;
function getPrimeNum()
{
let prime = [];
for (let i = 0; i<X; i++){
prime.push( true );
}
prime[0] = false ;
prime[1] = false ;
for (let p = 2; p * p <= prime.length; p++) {
if (prime[p]) {
for (let i = p * 2; i <= prime.length; i += p)
prime[i] = false ;
}
}
return prime;
}
function cntPairs( arr, N)
{
let isPrime
= getPrimeNum();
let cntOne = 0;
let cntPrime = 0;
for (let i = 0; i < N; i++) {
if (arr[i] == 1) {
cntOne += 1;
}
else if (isPrime[i]) {
cntPrime += 1;
}
}
let cntNonComp = 0;
cntNonComp = cntPrime * cntOne
+ Math.floor(cntOne * (cntOne - 1) / 2);
let res = 0;
res = N *Math.floor( (N - 1) / 2) - cntNonComp;
return res;
}
let arr = [ 1, 1, 2, 2, 8 ];
let N = arr.length;
document.write( cntPairs(arr, N));
</script>
|
Time Complexity: O(N + X × log(log(X))), Where X stores the maximum possible product of a pair in the given array.
Auxiliary Space: O(X)
Share your thoughts in the comments
Please Login to comment...