# Subsequence of size k with maximum possible GCD

We are given an array of positive integers and an integer k. Find the maximum possible GCD of a sub sequence of size k.

Examples:

```Input : arr[] = [2, 1, 4, 6] k = 3
Output : 2
GCD of [2, 4, 6] is 2

Input : arr[] = [1, 2, 3] k = 3
Output : 1
GCD of [1, 2, 3] is 1
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 Generate all the sub sequences of size k one by one and then find the GCD of all such generated sub sequences. Print the largest found GCD.

Method 2 In this method, we maintain a count array to store the count of divisors of every element. We will traverse the given array and for every element we will calculate its divisors and increment at the index of count array. The process of computing divisors will take O(sqrt(arr[i])) time, where arr[i] is element in the given array at index i. After the whole traversal, we can simply traverse the count array from last index to index 1. If we find an index with value equal to or greater than k, then this means that it is a divisor of at least k elements and also the max GCD.

 `// CPP program to find subsequence of size ` `// k with maximum possible GCD. ` `#include ` `using` `namespace` `std; ` ` `  `// function to find GCD of sub sequence of ` `// size k with max GCD in the array ` `int` `findMaxGCD(``int` `arr[], ``int` `n, ``int` `k) ` `{ ` `    ``// Computing highest element ` `    ``int` `high = *max_element(arr, arr+n); ` ` `  `    ``// Array to store the count of divisors ` `    ``// i.e. Potential GCDs ` `    ``int` `divisors[high + 1] = { 0 }; ` ` `  `    ``// Iterating over every element ` `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Calculating all the divisors ` `        ``for` `(``int` `j = 1; j <= ``sqrt``(arr[i]); j++) { ` ` `  `            ``// Divisor found ` `            ``if` `(arr[i] % j == 0) { ` ` `  `                ``// Incrementing count for divisor ` `                ``divisors[j]++; ` ` `  `                ``// Element/divisor is also a divisor ` `                ``// Checking if both divisors are ` `                ``// not same ` `                ``if` `(j != arr[i] / j) ` `                    ``divisors[arr[i] / j]++; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Checking the highest potential GCD ` `    ``for` `(``int` `i = high; i >= 1; i--) ` ` `  `        ``// If this divisor can divide at least k ` `        ``// numbers, it is a GCD of at least one ` `        ``// sub sequence of size k ` `        ``if` `(divisors[i] >= k) ` `            ``return` `i; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// Array in which sub sequence with size ` `    ``// k with max GCD is to be found ` `    ``int` `arr[] = { 1, 2, 4, 8, 8, 12 }; ` `    ``int` `k = 3; ` ` `  `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``cout << findMaxGCD(arr, n, k); ` `    ``return` `0; ` `} `

 `// Java program to find ` `// subsequence of size ` `// k with maximum possible GCD ` `import` `java .io.*; ` `import` `java .util.*; ` ` `  `class` `GFG ` `{ ` ` `  `// function to find GCD of  ` `// sub sequence of size k ` `// with max GCD in the array ` `static` `int` `findMaxGCD(``int` `[]arr, ` `                      ``int` `n, ``int` `k) ` `{ ` `    ``Arrays.sort(arr); ` `     `  `    ``// Computing highest element ` `    ``int` `high = arr[n - ``1``]; ` ` `  `    ``// Array to store the  ` `    ``// count of divisors ` `    ``// i.e. Potential GCDs ` `    ``int` `[]divisors = ``new` `int``[high + ``1``]; ` ` `  `    ``// Iterating over  ` `    ``// every element ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` ` `  `        ``// Calculating all the divisors ` `        ``for` `(``int` `j = ``1``;              ` `                 ``j <= Math.sqrt(arr[i]);  ` `                 ``j++) ` `        ``{ ` ` `  `            ``// Divisor found ` `            ``if` `(arr[i] % j == ``0``)  ` `            ``{ ` ` `  `                ``// Incrementing count ` `                ``// for divisor ` `                ``divisors[j]++; ` ` `  `                ``// Element/divisor is  ` `                ``// also a divisor Checking ` `                ``// if both divisors are ` `                ``// not same ` `                ``if` `(j != arr[i] / j) ` `                    ``divisors[arr[i] / j]++; ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Checking the highest ` `    ``// potential GCD ` `    ``for` `(``int` `i = high; i >= ``1``; i--) ` ` `  `        ``// If this divisor can divide  ` `        ``// at least k numbers, it is  ` `        ``// a GCD of at least one sub  ` `        ``// sequence of size k ` `        ``if` `(divisors[i] >= k) ` `            ``return` `i; ` `            ``return` `0` `; ` `} ` ` `  `// Driver code ` `static` `public` `void` `main (String[] args) ` `{ ` `    ``// Array in which sub sequence  ` `    ``// with size k with max GCD is  ` `    ``// to be found ` `    ``int` `[]arr = { ``1``, ``2``, ``4``,  ` `                  ``8``, ``8``, ``12` `}; ` `    ``int` `k = ``3``; ` ` `  `    ``int` `n = arr.length; ` `    ``System.out.println(findMaxGCD(arr, n, k)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by anuj_67. `

 `# Python 3 program to find subsequence  ` `# of size k with maximum possible GCD. ` `import` `math ` ` `  `# function to find GCD of sub sequence  ` `# of size k with max GCD in the array ` `def` `findMaxGCD(arr, n, k): ` ` `  `    ``# Computing highest element ` `    ``high ``=` `max``(arr) ` ` `  `    ``# Array to store the count of  ` `    ``# divisors i.e. Potential GCDs ` `    ``divisors ``=` `[``0``] ``*` `(high ``+` `1``) ` ` `  `    ``# Iterating over every element ` `    ``for` `i ``in` `range``(n) : ` ` `  `        ``# Calculating all the divisors ` `        ``for` `j ``in` `range``(``1``, ``int``(math.sqrt(arr[i])) ``+` `1``): ` ` `  `            ``# Divisor found ` `            ``if` `(arr[i] ``%` `j ``=``=` `0``) : ` ` `  `                ``# Incrementing count for divisor ` `                ``divisors[j] ``+``=` `1` ` `  `                ``# Element/divisor is also a divisor ` `                ``# Checking if both divisors are ` `                ``# not same ` `                ``if` `(j !``=` `arr[i] ``/``/` `j): ` `                    ``divisors[arr[i] ``/``/` `j] ``+``=` `1` ` `  `    ``# Checking the highest potential GCD ` `    ``for` `i ``in` `range``(high, ``0``, ``-``1``): ` ` `  `        ``# If this divisor can divide at least k ` `        ``# numbers, it is a GCD of at least one ` `        ``# sub sequence of size k ` `        ``if` `(divisors[i] >``=` `k): ` `            ``return` `i ` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``# Array in which sub sequence with size ` `    ``# k with max GCD is to be found ` `    ``arr ``=` `[ ``1``, ``2``, ``4``, ``8``, ``8``, ``12` `] ` `    ``k ``=` `3` ` `  `    ``n ``=` `len``(arr) ` `    ``print``(findMaxGCD(arr, n, k)) ` ` `  `# This code is contributed by ita_c `

 `// C# program to find subsequence of size ` `// k with maximum possible GCD ` `using` `System; ` `using` `System.Linq; ` ` `  `public` `class` `GFG { ` `     `  `    ``// function to find GCD of sub sequence of ` `    ``// size k with max GCD in the array ` `    ``static` `int` `findMaxGCD(``int` `[]arr, ``int` `n, ``int` `k) ` `    ``{ ` `        ``// Computing highest element ` `        ``int` `high = arr.Max(); ` `     `  `        ``// Array to store the count of divisors ` `        ``// i.e. Potential GCDs ` `        ``int` `[]divisors = ``new` `int``[high+1]; ` `     `  `        ``// Iterating over every element ` `        ``for` `(``int` `i = 0; i < n; i++) { ` `     `  `            ``// Calculating all the divisors ` `            ``for` `(``int` `j = 1; j <= Math.Sqrt(arr[i]); j++) ` `            ``{ ` `     `  `                ``// Divisor found ` `                ``if` `(arr[i] % j == 0) { ` `     `  `                    ``// Incrementing count for divisor ` `                    ``divisors[j]++; ` `     `  `                    ``// Element/divisor is also a divisor ` `                    ``// Checking if both divisors are ` `                    ``// not same ` `                    ``if` `(j != arr[i] / j) ` `                        ``divisors[arr[i] / j]++; ` `                ``} ` `            ``} ` `        ``} ` `     `  `        ``// Checking the highest potential GCD ` `        ``for` `(``int` `i = high; i >= 1; i--) ` `     `  `            ``// If this divisor can divide at least k ` `            ``// numbers, it is a GCD of at least one ` `            ``// sub sequence of size k ` `            ``if` `(divisors[i] >= k) ` `                ``return` `i; ` `                ``return` `0 ; ` `    ``} ` `     `  `    ``// Driver code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``// Array in which sub sequence with  ` `        ``// size k with max GCD is to be found ` `        ``int` `[]arr = { 1, 2, 4, 8, 8, 12 }; ` `        ``int` `k = 3; ` `     `  `        ``int` `n = arr.Length; ` `        ``Console.WriteLine(findMaxGCD(arr, n, k)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

Output:
```4
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

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.

Improved By : vt_m, chitranayal, Code_r, Akanksha_Rai

Article Tags :
Practice Tags :