Count of pairs in given Array whose GCD is not prime
Last Updated :
16 May, 2023
Given an array arr[] consisting of N positive integers, the task is to find the number of pairs such that the Greatest Common Divisor(GCD) of the pairs is not a prime number. The pair (i, j) and (j, i) are considered the same.
Examples:
Input: arr[] ={ 2, 3, 9}
Output: 10
Explanation:
Following are the possible pairs whose GCD is not prime:
- (0, 1): The GCD of arr[0](= 2) and arr[1](= 3) is 1.
- (0, 2): The GCD of arr[0](= 2) and arr[2](= 9) is 1.
Therefore, the total count of pairs is 2.
Input: arr[] = {3, 5, 2, 10}
Output: 4
Approach: The given problem can be solved by finding all the prime numbers till 105 and store them in a Set and then for each pair (i, j) if their GCD doesn’t lie in the set, then count this pair. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void primeSieve( bool * p)
{
for ( int i = 2; i * i <= 1000000; i++) {
if (p[i] == true ) {
for ( int j = i * 2;
j <= 1000000; j += i) {
p[j] = false ;
}
}
}
}
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int countPairs( int arr[], int n,
unordered_set< int > s)
{
int count = 0;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
int x = gcd(arr[i], arr[j]);
if (s.find(x) == s.end())
count++;
}
}
return count;
}
void countPairsUtil( int arr[], int n)
{
unordered_set< int > s;
bool p[1000005];
memset (p, true , sizeof (p));
primeSieve(p);
s.insert(2);
for ( int i = 3; i <= 1000000; i += 2)
if (p[i])
s.insert(i);
cout << countPairs(arr, n, s);
}
int main()
{
int arr[] = { 2, 3, 9 };
int N = sizeof (arr) / sizeof (arr[0]);
countPairsUtil(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void primeSieve( boolean [] p)
{
for ( int i = 2 ; i * i <= 1000000 ; i++) {
if (p[i] == true ) {
for ( int j = i * 2 ;
j <= 1000000 ; j += i) {
p[j] = false ;
}
}
}
}
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int countPairs( int arr[], int n,
HashSet<Integer> s)
{
int count = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
for ( int j = i + 1 ; j < n; j++) {
int x = gcd(arr[i], arr[j]);
if (!s.contains(x))
count++;
}
}
return count;
}
static void countPairsUtil( int arr[], int n)
{
HashSet<Integer> s = new HashSet<Integer>();
boolean []p = new boolean [ 1000005 ];
for ( int i= 0 ;i<p.length;i++)
p[i] = true ;
primeSieve(p);
s.add( 2 );
for ( int i = 3 ; i <= 1000000 ; i += 2 )
if (p[i])
s.add(i);
System.out.print(countPairs(arr, n, s));
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 9 };
int N = arr.length;
countPairsUtil(arr, N);
}
}
|
Python3
from math import sqrt,gcd
def primeSieve(p):
for i in range ( 2 , int (sqrt( 1000000 )), 1 ):
if (p[i] = = True ):
for j in range (i * 2 , 1000001 ,i):
p[j] = False
def countPairs(arr, n, s):
count = 0
for i in range (n - 1 ):
for j in range (i + 1 ,n, 1 ):
x = gcd(arr[i], arr[j])
if (x not in s):
count + = 1
return count
def countPairsUtil(arr, n):
s = set ()
p = [ True for i in range ( 1000005 )]
primeSieve(p)
s.add( 2 )
for i in range ( 3 , 1000001 , 2 ):
if (p[i]):
s.add(i)
print (countPairs(arr, n, s))
if __name__ = = '__main__' :
arr = [ 2 , 3 , 9 ]
N = len (arr)
countPairsUtil(arr, N)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static void primeSieve( bool [] p)
{
for ( int i = 2; i * i <= 1000000; i++) {
if (p[i] == true ) {
for ( int j = i * 2;
j <= 1000000; j += i) {
p[j] = false ;
}
}
}
}
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int countPairs( int []arr, int n,
HashSet< int > s)
{
int count = 0;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
int x = gcd(arr[i], arr[j]);
if (!s.Contains(x))
count++;
}
}
return count;
}
static void countPairsUtil( int []arr, int n)
{
HashSet< int > s = new HashSet< int >();
bool []p = new bool [1000005];
for ( int i = 0; i < p.Length; i++)
p[i] = true ;
primeSieve(p);
s.Add(2);
for ( int i = 3; i <= 1000000; i += 2)
if (p[i])
s.Add(i);
Console.Write(countPairs(arr, n, s));
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 9 };
int N = arr.Length;
countPairsUtil(arr, N);
}
}
|
Javascript
<script>
function primeSieve( p) {
for ( var i = 2; i * i <= 1000000; i++) {
if (p[i] == true ) {
for (j = i * 2; j <= 1000000; j += i) {
p[j] = false ;
}
}
}
}
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function countPairs(arr , n, s) {
var count = 0;
for ( var i = 0; i < n - 1; i++) {
for ( var j = i + 1; j < n; j++) {
var x = gcd(arr[i], arr[j]);
if (!s.has(x))
count++;
}
}
return count;
}
function countPairsUtil(arr, n)
{
var s = new Set();
var p = Array(1000005).fill( false );
for ( var i = 0; i < p.length; i++)
p[i] = true ;
primeSieve(p);
s.add(2);
for (i = 3; i <= 1000000; i += 2)
if (p[i])
s.add(i);
document.write(countPairs(arr, n, s));
}
var arr = [ 2, 3, 9 ];
var N = arr.length;
countPairsUtil(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...