Given an array, write a program to find the maximum GCD among all the subarray of the size >= 2 of the given array.

**Examples:**

Input list:[2, 3, 4, 4, 4]Output:4Input list:[3, 7, 2, 9, 18, 5, 1, 13 ]Output:9

### Approach:

- Import the math module for python
- Introduce a variable(say, V1) to store the gcd of each element of the list while looping through the list.
- Iterate through the elements of the array or list using a loop.
- At each iteration call the
**math.gcd() function.** - Store the outcome of the math.gcd() function to another variable (say, V2) at each iteration.
- Now compare V1 and V2. If V2 is greater than V1, set V1 equal to V2 else pass.
- Let the loop run through and print out the final value of V1.

Below you can find the implementation of the above-mentioned approach:**Examples 1:**

## Python3

`import` `math ` ` ` `# input list ` `List` `=` `[` `2` `, ` `3` `, ` `4` `, ` `4` `, ` `4` `] ` ` ` `max1 ` `=` `0` `for` `i ` `in` `range` `(` `len` `(` `List` `)` `-` `1` `): ` ` ` ` ` `# use math.gcd() function ` ` ` `gcd1 ` `=` `math.gcd(` `List` `[i], ` `List` `[i ` `+` `1` `]) ` ` ` `if` `(gcd1>max1): ` ` ` `max1 ` `=` `gcd1 ` ` ` `# print max1 ` `# as the result ` `print` `(max1)` |

*chevron_right*

*filter_none*

**Output:**

4

**Explanation:**

For the given array one of the subarrays having maximum gcd is[3, 5] which has gcd 4.

**Example 2:**

## Python3

`import` `math ` ` ` `# input list ` `List` `=` `[` `3` `, ` `7` `, ` `2` `, ` `9` `, ` `18` `, ` `5` `, ` `1` `, ` `13` `] ` ` ` `max1 ` `=` `0` `for` `i ` `in` `range` `(` `len` `(` `List` `)` `-` `1` `): ` ` ` ` ` `# use math.gcd() function ` ` ` `gcd1 ` `=` `math.gcd(` `List` `[i], ` `List` `[i ` `+` `1` `]) ` ` ` `if` `(gcd1>max1): ` ` ` `max1 ` `=` `gcd1 ` ` ` `# print max1 ` `# as the result ` `print` `(max1)` |

*chevron_right*

*filter_none*

**Output:**

9

**Explanation:**

For the given array one of the subarrays having maximum gcd is[4, 5] which has gcd 9.

## Recommended Posts:

- Smallest Subarray with given GCD
- Largest subarray with GCD one
- Smallest subarray with GCD as 1 | Segment Tree
- Length of the Smallest Subarray that must be removed in order to Maximise the GCD
- Length of longest subarray of length at least 2 with maximum GCD
- Program to find GCD or HCF of two numbers
- Program to find GCD of floating point numbers
- Program to find LCM of 2 numbers without using GCD
- Program to find GCD or HCF of two numbers using Middle School Procedure
- Program to find Greatest Common Divisor (GCD) of N strings
- Python Program for GCD of more than two (or array) numbers
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Maximum length of subarray such that sum of the subarray is even
- Count of subarray that does not contain any subarray with sum 0
- Maximum length of subarray such that all elements are equal in the subarray
- First subarray having sum at least half the maximum sum of any subarray of size K
- Recursive program to print formula for GCD of n integers
- C++ Program for GCD of more than two (or array) numbers
- Java Program for GCD of more than two (or array) numbers
- Given GCD G and LCM L, find number of possible pairs (a, b)

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.