# Longest sub-sequence with maximum GCD

Given an array arr[] of length N, the task is to find the length of the longest sub-sequence with maximum possible GCD.

Examples:

Input: arr[] = {2, 1, 2}
Output: 2
{2}, {2} and {2, 2} are the subsequences
with the maximum possible GCD.

Input: arr[] = {1, 2, 3}
Output: 1
{3} is the required subsequence.

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

Approach: The maximum possible GCD from the array will be equal to the value of the largest element in the array. Now, to maximize the length of the resulting subsequence, find the number of elements with a value equal to this largest value in the array and the count of these elements is the required answer.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the length ` `// of the largest subsequence with ` `// maximum possible GCD ` `int` `maxLen(``int``* arr, ``int` `n) ` `{ ` `    ``// Maximum value from the array ` `    ``int` `max_val = *max_element(arr, arr + n); ` ` `  `    ``// To store the frequency of the ` `    ``// maximum element in the array ` `    ``int` `freq = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// If current element is equal ` `        ``// to the maximum element ` `        ``if` `(arr[i] == max_val) ` `            ``freq++; ` `    ``} ` ` `  `    ``return` `freq; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 3, 2, 2, 3, 3, 3 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(``int``); ` ` `  `    ``cout << maxLen(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.Arrays; ` ` `  `class` `GFG ` `{ ` ` `  `// Function to return the length ` `// of the largest subsequence with ` `// maximum possible GCD ` `static` `int` `maxLen(``int``[] arr, ``int` `n) ` `{ ` `    ``// Maximum value from the array ` `    ``int` `max_val = Arrays.stream(arr).max().getAsInt(); ` ` `  `    ``// To store the frequency of the ` `    ``// maximum element in the array ` `    ``int` `freq = ``0``; ` ` `  `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `    ``{ ` ` `  `        ``// If current element is equal ` `        ``// to the maximum element ` `        ``if` `(arr[i] == max_val) ` `            ``freq++; ` `    ``} ` `    ``return` `freq; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String []args)  ` `{ ` `    ``int` `arr[] = { ``3``, ``2``, ``2``, ``3``, ``3``, ``3` `}; ` `    ``int` `n = arr.length; ` ` `  `    ``System.out.println(maxLen(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to return the length  ` `# of the largest subsequence with  ` `# maximum possible GCD  ` `def` `maxLen(arr, n) : ` `     `  `    ``# Maximum value from the array  ` `    ``max_val ``=` `max``(arr);  ` ` `  `    ``# To store the frequency of the  ` `    ``# maximum element in the array  ` `    ``freq ``=` `0``;  ` ` `  `    ``for` `i ``in` `range``(n) : ` ` `  `        ``# If current element is equal  ` `        ``# to the maximum element  ` `        ``if` `(arr[i] ``=``=` `max_val) : ` `            ``freq ``+``=` `1``;  ` ` `  `    ``return` `freq;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``arr ``=` `[ ``3``, ``2``, ``2``, ``3``, ``3``, ``3` `];  ` `    ``n ``=` `len``(arr);  ` ` `  `    ``print``(maxLen(arr, n));  ` ` `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the approach ` `using` `System;  ` `using` `System.Linq; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to return the length ` `// of the largest subsequence with ` `// maximum possible GCD ` `static` `int` `maxLen(``int``[] arr, ``int` `n) ` `{ ` `    ``// Maximum value from the array ` `    ``int` `max_val = arr.Max(); ` ` `  `    ``// To store the frequency of the ` `    ``// maximum element in the array ` `    ``int` `freq = 0; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` ` `  `        ``// If current element is equal ` `        ``// to the maximum element ` `        ``if` `(arr[i] == max_val) ` `            ``freq++; ` `    ``} ` `    ``return` `freq; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args)  ` `{ ` `    ``int` `[]arr = { 3, 2, 2, 3, 3, 3 }; ` `    ``int` `n = arr.Length; ` ` `  `    ``Console.WriteLine(maxLen(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```4
```

My Personal Notes arrow_drop_up 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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.