Python program to Find the Jumbo GCD subarray
Last Updated :
27 Jan, 2023
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:
- 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)
Output:
4
Explanation: For the given array one of the subarrays having maximum gcd is[3, 5] which has gcd 4.
Time Complexity: O(n * log(min(a, b))), as it iterates through the input list once of size n, and gcd() method takes log(min(a,b)) time.
Auxiliary Space: O(log(n)), as it uses recursion and the maximum depth of recursion is log(n).
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)
Output:
9
Explanation:
For the given array one of the subarrays having maximum gcd is[4, 5] which has gcd 9.
Time Complexity: O(n * log(min(a, b))), as it iterates through the input list once of size n, and gcd() method takes log(min(a,b)) time.
Auxiliary Space: O(log(n)), as it uses recursion and the maximum depth of recursion is log(n).
Similar Reads
Python Program to Find the Gcd of Two Numbers The task of finding the GCD (Greatest Common Divisor) of two numbers in Python involves determining the largest number that divides both input values without leaving a remainder. For example, if a = 60 and b = 48, the GCD is 12, as 12 is the largest number that divides both 60 and 48 evenly. Using e
2 min read
Python3 Program for Range LCM Queries Given an array of integers, evaluate queries of the form LCM(l, r). There might be many queries, hence evaluate the queries efficiently. LCM (l, r) denotes the LCM of array elements that lie between the index l and r (inclusive of both indices) Mathematically, LCM(l, r) = LCM(arr[l], arr[l+1] , ....
4 min read
Python Program for Find remainder of array multiplication divided by n Write a Python program for a given multiple numbers and a number n, the task is to print the remainder after multiplying all the numbers divided by n. Examples: Input: arr[] = {100, 10, 5, 25, 35, 14}, n = 11Output: 9Explanation: 100 x 10 x 5 x 25 x 35 x 14 = 61250000 % 11 = 9Input : arr[] = {100, 1
3 min read
Python Program for GCD of more than two (or array) numbers The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers. gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) = gcd(gcd(a, c), b) Python # GCD of more than two (or array) numbe
1 min read
Number of subarrays with GCD = 1 | Segment tree Given an array arr[], the task is to find the count of sub-arrays with GCD equal to 1.Examples: Input: arr[] = {1, 1, 1} Output: 6 Every single subarray of the given array has GCD of 1 and there are a total of 6 subarrays.Input: arr[] = {2, 2, 2} Output: 0 Approach: This problem can be solved in O(N
10 min read