Python program to Find the Jumbo GCD subarray

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: 4

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

Approach:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

Output:

4

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



Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

Output:

9

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

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.





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 :