Count of triplets in a given Array having GCD K
Last Updated :
01 Nov, 2021
Given an integer array arr[] and an integer K, the task is to count all triplets whose GCD is equal to K.
Examples:
Input: arr[] = {1, 4, 8, 14, 20}, K = 2
Output: 3
Explanation:
Triplets (4, 14, 20), (8, 14, 20) and (4, 8, 14) have GCD equal to 2.
Input: arr[] = {1, 2, 3, 4, 5}, K = 7
Output: 0
Approach:
Follow the steps below to solve the problem:
- Maintain an array cnt[i] which denotes the numbers of triplet in array with GCD = i.
- Now, to find cnt[i], first of all count all the multiples of i in the array which is stored in another array mul[i].
- Now select any three values from mul[i]. Number of ways of selecting three values is mul [i] C 3, store this value in cnt[i].
- But it also includes the triplets with GCD a multiple of i.So we Again iterate over all the multiples of i and subtract cnt[j] from cnt[i] where j is a multiple of i.
- Finally return cnt[K].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 1;
int freq[MAXN] = { 0 };
int mul[MAXN] = { 0 };
int cnt[MAXN] = { 0 };
int nC3( int n)
{
if (n < 3)
return 0;
return (n * (n - 1) * (n - 2)) / 6;
}
void count_triplet(vector< int > arr,
int N, int K)
{
for ( int i = 0; i < N; i++) {
freq[arr[i]]++;
}
for ( int i = 1; i <= 1000000; i++) {
for ( int j = i; j <= 1000000;
j += i) {
mul[i] += freq[j];
}
cnt[i] = nC3(mul[i]);
}
for ( int i = 1000000; i >= 1; i--) {
for ( int j = 2 * i; j <= 1000000;
j += i) {
cnt[i] -= cnt[j];
}
}
cout << "Number of triplets "
<< "with GCD " << K;
cout << " are " << cnt[K];
}
int main()
{
vector< int > arr = { 1, 7, 12, 6,
15, 9 };
int N = 6, K = 3;
count_triplet(arr, N, K);
return 0;
}
|
Java
class GFG{
static int MAXN = 1000001 ;
static int freq[] = new int [MAXN];
static int mul[] = new int [MAXN];
static int cnt[] = new int [MAXN];
static int nC3( int n)
{
if (n < 3 )
return 0 ;
return (n * (n - 1 ) * (n - 2 )) / 6 ;
}
static void count_triplet( int [] arr,
int N, int K)
{
int i, j;
for (i = 0 ; i < N; i++)
{
freq[arr[i]]++;
}
for (i = 1 ; i <= 1000000 ; i++)
{
for (j = i; j <= 1000000 ; j += i)
{
mul[i] += freq[j];
}
cnt[i] = nC3(mul[i]);
}
for (i = 1000000 ; i >= 1 ; i--)
{
for (j = 2 * i; j <= 1000000 ; j += i)
{
cnt[i] -= cnt[j];
}
}
System.out.print( "Number of triplets " +
"with GCD " + K);
System.out.print( " are " + cnt[K]);
}
public static void main (String []args)
{
int []arr = { 1 , 7 , 12 , 6 , 15 , 9 };
int N = 6 , K = 3 ;
count_triplet(arr, N, K);
}
}
|
Python3
MAXN = int ( 1e6 + 1 )
freq = [ 0 ] * MAXN
mul = [ 0 ] * MAXN
cnt = [ 0 ] * MAXN
def nC3(n):
if (n < 3 ):
return 0
return (n * (n - 1 ) * (n - 2 )) / 6
def count_triplet(arr, N, K):
for i in range (N):
freq[arr[i]] + = 1
for i in range ( 1 , 1000000 + 1 ):
for j in range (i, 1000000 + 1 , i):
mul[i] + = freq[j]
cnt[i] = nC3(mul[i])
for i in range ( 1000000 , 0 , - 1 ):
for j in range ( 2 * i, 1000000 + 1 , i):
cnt[i] - = cnt[j]
print ( "Number of triplets with GCD"
" {0} are {1}" . format (K, int (cnt[K])))
if __name__ = = '__main__' :
arr = [ 1 , 7 , 12 , 6 , 15 , 9 ]
N = 6
K = 3
count_triplet(arr, N, K)
|
C#
using System;
class GFG{
static int MAXN = 1000001;
static int []freq = new int [MAXN];
static int []mul = new int [MAXN];
static int []cnt = new int [MAXN];
static int nC3( int n)
{
if (n < 3)
return 0;
return (n * (n - 1) * (n - 2)) / 6;
}
static void count_triplet( int [] arr,
int N, int K)
{
int i, j;
for (i = 0; i < N; i++)
{
freq[arr[i]]++;
}
for (i = 1; i <= 1000000; i++)
{
for (j = i; j <= 1000000; j += i)
{
mul[i] += freq[j];
}
cnt[i] = nC3(mul[i]);
}
for (i = 1000000; i >= 1; i--)
{
for (j = 2 * i; j <= 1000000; j += i)
{
cnt[i] -= cnt[j];
}
}
Console.Write( "Number of triplets " +
"with GCD " + K);
Console.Write( " are " + cnt[K]);
}
public static void Main ( string []args)
{
int []arr = { 1, 7, 12, 6, 15, 9 };
int N = 6, K = 3;
count_triplet(arr, N, K);
}
}
|
Javascript
<script>
var MAXN = 1000001;
var freq = Array(MAXN).fill(0);
var mul = Array(MAXN).fill(0);
var cnt = Array(MAXN).fill(0);
function nC3(n)
{
if (n < 3)
return 0;
return (n * (n - 1) * (n - 2)) / 6;
}
function count_triplet(arr, N, K)
{
for ( var i = 0; i < N; i++) {
freq[arr[i]]++;
}
for ( var i = 1; i <= 1000000; i++) {
for ( var j = i; j <= 1000000;
j += i) {
mul[i] += freq[j];
}
cnt[i] = nC3(mul[i]);
}
for ( var i = 1000000; i >= 1; i--) {
for ( var j = 2 * i; j <= 1000000;
j += i) {
cnt[i] -= cnt[j];
}
}
document.write( "Number of triplets "
+ "with GCD " + K);
document.write( " are " + cnt[K]);
}
var arr = [ 1, 7, 12, 6,
15, 9 ];
var N = 6, K = 3;
count_triplet(arr, N, K);
</script>
|
Output:
Number of triplets with GCD 3 are 4
Time Complexity: O (N * log N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...