# Length of longest subarray of length at least 2 with maximum GCD

Given an array arr[] of length N, the task is the find the length of longest subarray of length at least 2 with maximum possible GCD value.
Examples:

Input: arr[] = {1, 2, 2}
Output:
Explanation:
Possible sub-arrays of size greater than 2 and there GCD’s are:
1) {1, 2} -> 1
2) {2, 2} -> 2
3) {1, 2, 3} -> 1
Here, the maximum GCD value is 2 and longest sub-array having GCD = 2 is {2, 2}.
Hence the answer is {2, 2}.
Input: arr[] = {18, 3, 6, 9}
Output:
Explanation:
Here, the maximum GCD value is 3 and longest sub-array having GCD = 3 is {18, 3, 6, 9}.
Hence the answer is {18, 3, 6, 9}.

Naive Approach: The idea is to generate all the possible subarray of size at least 2 and find the GCD of each subarray of them individually. Then, the length of the subarray with maximum GCD value is the required length.
Time Complexity: O(N2)
Efficient Approach:

1. Find the maximum GCD(say g) of all the subarray with length atleast 2 by using the approach discussed in this article.
2. Traverse the given array and count the maximum number of consecutive elements which are divisible by GCD g.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach  ` ` `  `#include   ` `using` `namespace` `std;  ` ` `  `// Function to calculate GCD of two numbers  ` `int` `gcd(``int` `a, ``int` `b)  ` `{  ` `    ``if` `(b == 0)  ` `        ``return` `a;  ` `    ``return` `gcd(b, a % b);  ` `}  ` ` `  `// Function to find maximum size subarray  ` `// having maximum GCD  ` `int` `maximumGcdSubarray(``int` `arr[], ``int` `n)  ` `{  ` `    ``// Base Case  ` `    ``if` `(n == 1)  ` `        ``return` `0;  ` ` `  `    ``// Let the maximum GCD be 1 initially  ` `    ``int` `k = 1;  ` ` `  `    ``// Loop thourgh array to find maximum  ` `    ``// GCD of subarray with size 2  ` `    ``for` `(``int` `i = 1; i < n; ++i) {  ` `        ``k = max(k, gcd(arr[i], arr[i - 1]));  ` `    ``}  ` ` `  `    ``int` `cnt = 0;  ` `    ``int` `maxLength = 0;  ` ` `  `    ``// Traverse the array  ` `    ``for` `(``int` `i = 0; i < n; i++) {  ` ` `  `        ``// Is a multiple of k, increase cnt  ` `        ``if` `(arr[i] % k == 0) {  ` `            ``cnt++;  ` `        ``}  ` ` `  `        ``// Else update maximum length with  ` `        ``// consecutive element divisible by k  ` `        ``// Set cnt to 0  ` `        ``else` `{  ` `            ``maxLength = max(maxLength, cnt);  ` `            ``cnt = 0;  ` `        ``}  ` `    ``}  ` ` `  `    ``// Update the maxLength  ` `    ``maxLength = max(maxLength, cnt);  ` ` `  `    ``// Return the maxLength  ` `    ``return` `maxLength;  ` `}  ` ` `  `// Driver Code  ` `int` `main()  ` `{  ` `    ``int` `arr[] = { 18, 3, 6, 9 };  ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);  ` ` `  `    ``// Function Call  ` `    ``cout << maximumGcdSubarray(arr, n);  ` `    ``return` `0;  ` `}  `

## Java

 `// Java program for the above approach ` `class` `GFG{ ` `     `  `// Function to calculate GCD of  ` `// two numbers ` `static` `int` `gcd(``int` `a, ``int` `b) ` `{ ` `    ``if` `(b == ``0``) ` `        ``return` `a; ` `    ``return` `gcd(b, a % b); ` `} ` `     `  `// Function to find maximum size   ` `// subarray having maximum GCD  ` `static` `int` `maximumGcdSubarray(``int` `arr[], ``int` `n) ` `{ ` `     `  `    ``// Base case ` `    ``if` `(n == ``1``) ` `        ``return` `0``; ` `         `  `    ``// Let the maximum GCD be 1 initially ` `    ``int` `k = ``1``; ` `         `  `    ``// Loop through array to find maximum  ` `    ``// GCD of subarray with size 2 ` `    ``for``(``int` `i = ``1``; i < n; i++) ` `    ``{ ` `       ``k = Math.max(k, gcd(arr[i], arr[i - ``1``])); ` `    ``} ` `         `  `    ``int` `cnt = ``0``; ` `    ``int` `maxLength = ``0``; ` `         `  `    ``// Traverse the array ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `        `  `       ``// Is a multiple of k, increase cnt ` `       ``if``(arr[i] % k == ``0``) ` `       ``{ ` `           ``cnt++; ` `       ``} ` `        `  `       ``// Else update maximum length with  ` `       ``// consecutive element divisible by k  ` `       ``// Set cnt to 0  ` `       ``else` `       ``{ ` `           ``maxLength = Math.max(maxLength, cnt); ` `           ``cnt = ``0``; ` `       ``} ` `    ``} ` `     `  `    ``// Update the maxLength ` `    ``maxLength = Math.max(maxLength, cnt); ` `         `  `    ``// Return the maxLength ` `    ``return` `maxLength; ` `} ` `     `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``18``, ``3``, ``6``, ``9` `}; ` `    ``int` `n = arr.length; ` `         `  `    ``// Function call ` `    ``System.out.println(maximumGcdSubarray(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by stutipathak31jan `

## Python3

 `# Python3 program for the above approach  ` ` `  `# Function to calculate GCD of  ` `# two numbers ` `def` `gcd(a, b): ` `     `  `    ``if` `b ``=``=` `0``: ` `        ``return` `a ` `    ``return` `gcd(b, a ``%` `b) ` ` `  `# Function to find maximum size  ` `# subarray having maximum GCD  ` `def` `maxGcdSubarray(arr, n): ` `     `  `    ``# Base case ` `    ``if` `n ``=``=` `1``: ` `        ``return` `0` `     `  `    ``# Let the maximum GCD be 1 initially ` `    ``k ``=` `1` `     `  `    ``# Loop through array to find maximum  ` `    ``# GCD of subarray with size 2 ` `    ``for` `i ``in` `range``(``1``, n): ` `        ``k ``=` `max``(k, gcd(arr[i], arr[i ``-` `1``])) ` `         `  `    ``cnt ``=` `0` `    ``maxLength ``=` `0` `     `  `    ``# Traverse the array ` `    ``for` `i ``in` `range``(n): ` `         `  `        ``# Is a multiple of k, increase cnt ` `        ``if` `arr[i] ``%` `k ``=``=` `0``: ` `            ``cnt ``+``=` `1` `         `  `        ``# Else update maximum length with  ` `        ``# consecutive element divisible by k  ` `        ``# Set cnt to 0  ` `        ``else``: ` `            ``maxLength ``=` `max``(maxLength, cnt) ` `            ``cnt ``=` `0` `             `  `    ``# Update the maxLength ` `    ``maxLength ``=` `max``(maxLength, cnt) ` `     `  `    ``# Return the maxLength ` `    ``return` `maxLength ` ` `  `# Driver Code ` `arr ``=` `[ ``18``, ``3``, ``6``, ``9` `] ` `n ``=` `len``(arr) ` `     `  `# Function call ` `print``(maxGcdSubarray(arr, n)) ` ` `  `# This code is contributed by stutipathak31jan `

## C#

 `// C# program for the above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG{ ` `     `  `// Function to calculate GCD of  ` `// two numbers  ` `static` `int` `gcd(``int` `a, ``int` `b)  ` `{ ` `    ``if` `(b == 0)  ` `        ``return` `a;  ` `    ``return` `gcd(b, a % b);  ` `}  ` `     `  `// Function to find maximum size  ` `// subarray having maximum GCD  ` `static` `int` `maximumGcdSubarray(``int``[] arr, ``int` `n)  ` `{  ` `     `  `    ``// Base case  ` `    ``if` `(n == 1)  ` `        ``return` `0;  ` `         `  `    ``// Let the maximum GCD be 1 initially  ` `    ``int` `k = 1;  ` `         `  `    ``// Loop through array to find maximum  ` `    ``// GCD of subarray with size 2  ` `    ``for``(``int` `i = 1; i < n; i++)  ` `    ``{  ` `        ``k = Math.Max(k, gcd(arr[i], arr[i - 1]));  ` `    ``}  ` `         `  `    ``int` `cnt = 0;  ` `    ``int` `maxLength = 0;  ` `         `  `    ``// Traverse the array  ` `    ``for``(``int` `i = 0; i < n; i++)  ` `    ``{  ` `         `  `        ``// Is a multiple of k, increase cnt  ` `        ``if``(arr[i] % k == 0)  ` `        ``{  ` `            ``cnt++;  ` `        ``}  ` `             `  `        ``// Else update maximum length with  ` `        ``// consecutive element divisible by k  ` `        ``// Set cnt to 0  ` `        ``else` `        ``{  ` `            ``maxLength = Math.Max(maxLength, cnt);  ` `            ``cnt = 0;  ` `        ``}  ` `    ``}  ` `     `  `    ``// Update the maxLength  ` `    ``maxLength = Math.Max(maxLength, cnt);  ` `         `  `    ``// Return the maxLength  ` `    ``return` `maxLength;  ` `}  ` ` `  `// Driver code     ` `static` `void` `Main() ` `{ ` `    ``int``[] arr = { 18, 3, 6, 9 };  ` `    ``int` `n = arr.Length;  ` `         `  `    ``// Function call  ` `    ``Console.WriteLine(maximumGcdSubarray(arr, n)); ` `} ` `} ` ` `  `// This code is contributed by divyeshrabadiya07 `

Output:

```4
```

Time Complexity: O(N), where N is the length of the array.

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.

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.