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 = 2Output:3Explanation:

Triplets (4, 14, 20), (8, 14, 20) and (4, 8, 14) have GCD equal to 2.Input:arr[] = {1, 2, 3, 4, 5}, K = 7Output: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, store this value in^{mul [i]}C_{3}**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**. - Finaly return
**cnt[K]**.

Below is the implementation of the above approach:

## C++

`// C++ program to count the` `// number of triplets in the` `// array with GCD equal to K` `#include <bits/stdc++.h>` `using` `namespace` `std;` `const` `int` `MAXN = 1e6 + 1;` `// frequency array` `int` `freq[MAXN] = { 0 };` `// mul[i] stores the count` `// of multiples of i` `int` `mul[MAXN] = { 0 };` `// cnt[i] stores the count` `// of triplets with gcd = i` `int` `cnt[MAXN] = { 0 };` `// Return nC3` `int` `nC3(` `int` `n)` `{` ` ` `if` `(n < 3)` ` ` `return` `0;` ` ` `return` `(n * (n - 1) * (n - 2)) / 6;` `}` `// Function to count and return` `// the number of triplets in the` `// array with GCD equal to K` `void` `count_triplet(vector<` `int` `> arr,` ` ` `int` `N, ` `int` `K)` `{` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// Store frequency of` ` ` `// array elements` ` ` `freq[arr[i]]++;` ` ` `}` ` ` `for` `(` `int` `i = 1; i <= 1000000; i++) {` ` ` `for` `(` `int` `j = i; j <= 1000000;` ` ` `j += i) {` ` ` `// Store the multiples of` ` ` `// i present in the array` ` ` `mul[i] += freq[j];` ` ` `}` ` ` `// Count triplets with gcd` ` ` `// equal to any multiple of i` ` ` `cnt[i] = nC3(mul[i]);` ` ` `}` ` ` `// Remove all triplets which have gcd` ` ` `// equal to a multiple of 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];` `}` `// Driver Program` `int` `main()` `{` ` ` `vector<` `int` `> arr = { 1, 7, 12, 6,` ` ` `15, 9 };` ` ` `int` `N = 6, K = 3;` ` ` `count_triplet(arr, N, K);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the` `// number of triplets in the` `// array with GCD equal to K` `class` `GFG{` ` ` `static` `int` `MAXN = ` `1000001` `;` `// Frequency array` `static` `int` `freq[] = ` `new` `int` `[MAXN];` `// mul[i] stores the count` `// of multiples of i` `static` `int` `mul[] = ` `new` `int` `[MAXN];` `// cnt[i] stores the count` `// of triplets with gcd = i` `static` `int` `cnt[] = ` `new` `int` `[MAXN];` `// Return nC3` `static` `int` `nC3(` `int` `n)` `{` ` ` `if` `(n < ` `3` `)` ` ` `return` `0` `;` ` ` ` ` `return` `(n * (n - ` `1` `) * (n - ` `2` `)) / ` `6` `;` `}` `// Function to count and return` `// the number of triplets in the` `// array with GCD equal to K` `static` `void` `count_triplet(` `int` `[] arr,` ` ` `int` `N, ` `int` `K)` `{ ` ` ` `int` `i, j;` ` ` `for` `(i = ` `0` `; i < N; i++)` ` ` `{` ` ` ` ` `// Store frequency of` ` ` `// array elements` ` ` `freq[arr[i]]++;` ` ` `}` ` ` ` ` `for` `(i = ` `1` `; i <= ` `1000000` `; i++)` ` ` `{` ` ` `for` `(j = i; j <= ` `1000000` `; j += i)` ` ` `{` ` ` ` ` `// Store the multiples of` ` ` `// i present in the array` ` ` `mul[i] += freq[j];` ` ` `}` ` ` ` ` `// Count triplets with gcd` ` ` `// equal to any multiple of i` ` ` `cnt[i] = nC3(mul[i]);` ` ` `}` ` ` `// Remove all triplets which have gcd` ` ` `// equal to a multiple of 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]);` `}` `// Driver code` `public` `static` `void` `main (String []args)` `{` ` ` `int` `[]arr = { ` `1` `, ` `7` `, ` `12` `, ` `6` `, ` `15` `, ` `9` `};` ` ` `int` `N = ` `6` `, K = ` `3` `;` ` ` ` ` `count_triplet(arr, N, K);` `} ` `}` `// This code is contributed by chitranayal` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count the number of ` `# triplets in the array with GCD equal to K ` `MAXN ` `=` `int` `(` `1e6` `+` `1` `)` `# Frequency array` `freq ` `=` `[` `0` `] ` `*` `MAXN` `# mul[i] stores the count` `# of multiples of i` `mul ` `=` `[` `0` `] ` `*` `MAXN` `# cnt[i] stores the count` `# of triplets with gcd = i` `cnt ` `=` `[` `0` `] ` `*` `MAXN` `# Return nC3` `def` `nC3(n):` ` ` `if` `(n < ` `3` `):` ` ` `return` `0` ` ` `return` `(n ` `*` `(n ` `-` `1` `) ` `*` `(n ` `-` `2` `)) ` `/` `6` `# Function to count and return` `# the number of triplets in the` `# array with GCD equal to K` `def` `count_triplet(arr, N, K):` ` ` `for` `i ` `in` `range` `(N):` ` ` `# Store frequency of` ` ` `# array elements` ` ` `freq[arr[i]] ` `+` `=` `1` ` ` `for` `i ` `in` `range` `(` `1` `, ` `1000000` `+` `1` `):` ` ` `for` `j ` `in` `range` `(i, ` `1000000` `+` `1` `, i):` ` ` `# Store the multiples of` ` ` `# i present in the array` ` ` `mul[i] ` `+` `=` `freq[j]` ` ` `# Count triplets with gcd` ` ` `# equal to any multiple of i` ` ` `cnt[i] ` `=` `nC3(mul[i])` ` ` `# Remove all triplets which have gcd` ` ` `# equal to a multiple of 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])))` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `arr ` `=` `[ ` `1` `, ` `7` `, ` `12` `, ` `6` `, ` `15` `, ` `9` `]` ` ` `N ` `=` `6` ` ` `K ` `=` `3` ` ` `count_triplet(arr, N, K)` `# This code is contributed by Shivam Singh` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the` `// number of triplets in the` `// array with GCD equal to K` `using` `System;` `class` `GFG{` ` ` `static` `int` `MAXN = 1000001;` ` ` `// Frequency array` `static` `int` `[]freq = ` `new` `int` `[MAXN];` ` ` `// mul[i] stores the count` `// of multiples of i` `static` `int` `[]mul = ` `new` `int` `[MAXN];` ` ` `// cnt[i] stores the count` `// of triplets with gcd = i` `static` `int` `[]cnt = ` `new` `int` `[MAXN];` ` ` `// Return nC3` `static` `int` `nC3(` `int` `n)` `{` ` ` `if` `(n < 3)` ` ` `return` `0;` ` ` ` ` `return` `(n * (n - 1) * (n - 2)) / 6;` `}` ` ` `// Function to count and return` `// the number of triplets in the` `// array with GCD equal to K` `static` `void` `count_triplet(` `int` `[] arr,` ` ` `int` `N, ` `int` `K)` `{ ` ` ` `int` `i, j;` ` ` `for` `(i = 0; i < N; i++)` ` ` `{` ` ` ` ` `// Store frequency of` ` ` `// array elements` ` ` `freq[arr[i]]++;` ` ` `}` ` ` ` ` `for` `(i = 1; i <= 1000000; i++)` ` ` `{` ` ` `for` `(j = i; j <= 1000000; j += i)` ` ` `{` ` ` ` ` `// Store the multiples of` ` ` `// i present in the array` ` ` `mul[i] += freq[j];` ` ` `}` ` ` ` ` `// Count triplets with gcd` ` ` `// equal to any multiple of i` ` ` `cnt[i] = nC3(mul[i]);` ` ` `}` ` ` ` ` `// Remove all triplets which have gcd` ` ` `// equal to a multiple of 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]);` `}` ` ` `// Driver code` `public` `static` `void` `Main (` `string` `[]args)` `{` ` ` `int` `[]arr = { 1, 7, 12, 6, 15, 9 };` ` ` `int` `N = 6, K = 3;` ` ` ` ` `count_triplet(arr, N, K);` `} ` `}` ` ` `// This code is contributed by Ritik Bansal` |

*chevron_right*

*filter_none*

**Output:**

Number of triplets with GCD 3 are 4

**Time Complexity: **O (N * log N) **Auxiliary Space: **O(N)

## Recommended Posts:

- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count numbers in a range having GCD of powers of prime factors equal to 1
- Count of quadruplets from range [L, R] having GCD equal to K
- Count of all possible pairs having sum of LCM and GCD equal to N
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
- Count number of triplets with product equal to given number with duplicates allowed
- Count of triplets in an Array (i, j, k) such that i < j < k and a[k] < a[i] < a[j]
- Largest subsequence having GCD greater than 1
- Segregate groups of first N numbers having GCD equals to 1
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Minimum length of subsequence having unit GCD
- Array with GCD of any of its subset belongs to the given array
- Count ways to form minimum product triplets
- Count non-negative triplets with sum equal to N
- Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X
- Count of triplets of numbers 1 to N such that middle element is always largest
- Count of ways to split N into Triplets forming a Triangle
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
- Count pairs of natural numbers with GCD equal to given number

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.