Maximum GCD of all subarrays of length at least 2
Given an array arr[] of N numbers. The task is to find the maximum GCD of all subarrays of size greater than 1.
Examples:
Input: arr[] = { 3, 18, 9, 9, 5, 15, 8, 7, 6, 9 }
Output: 9
Explanation:
GCD of the subarray {18, 9, 9} is maximum which is 9.
Input: arr[] = { 4, 8, 12, 16, 20, 24 }
Output: 4
Explanation:
GCD of the subarray {4, 18, 12, 16, 20, 24} is maximum which is 4.
Naive Approach: The idea is to generate all the subarray of size greater than 1 and then find the maximum of gcd of all subarray formed.
Time complexity: O(N2)
Efficient Approach: Let GCD of two numbers be g. Now if we take gcd of g with any third number say c then, gcd will decrease or remain same, but it will never increase.
The idea is to find gcd of every consecutive pair in the arr[] and the maximum of gcd of all the pairs formed is the desired result.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find GCD int gcd(int a, int b) { if (b == 0) { return a; } return gcd(b, a % b); } void findMaxGCD(int arr[], int n) { // To store the maximum GCD int maxGCD = 0; // Traverse the array for (int i = 0; i < n - 1; i++) { // Find GCD of the consecutive // element int val = gcd(arr[i], arr[i + 1]); // If calculated GCD > maxGCD // then update it if (val > maxGCD) { maxGCD = val; } } // Print the maximum GCD cout << maxGCD << endl; } // Driver Code int main() { int arr[] = { 3, 18, 9, 9, 5, 15, 8, 7, 6, 9 }; int n = sizeof(arr) / sizeof(arr[0]); // Function Call findMaxGCD(arr, n); return 0; } |
9
Time Complexity: O(N), where N is the length of the array.
Recommended Posts:
- Maximum length L such that the sum of all subarrays of length L is less than K
- Maximum sum of non-overlapping subarrays of length atmost K
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Count of possible subarrays and subsequences using given length of Array
- Count of Subarrays in an array containing numbers from 1 to the length of subarray
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Maximum of all Subarrays of size k using set in C++ STL
- Number of subarrays whose minimum and maximum are same
- Sum of maximum of all subarrays | Divide and Conquer
- Maximum sum two non-overlapping subarrays of given size
- Number of subarrays with maximum values in given range
- Count of subarrays whose maximum element is greater than k
- Sum of minimum and maximum elements of all subarrays of size k.
- Maximum sum of lengths of non-overlapping subarrays with k as the max element.
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.

