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++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
void findMaxGCD( int arr[], int n)
{
int maxGCD = 0;
for ( int i = 0; i < n - 1; i++) {
int val = gcd(arr[i], arr[i + 1]);
if (val > maxGCD) {
maxGCD = val;
}
}
cout << maxGCD << endl;
}
int main()
{
int arr[] = { 3, 18, 9, 9, 5,
15, 8, 7, 6, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
findMaxGCD(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int gcd( int a, int b)
{
if (b == 0 )
{
return a;
}
return gcd(b, a % b);
}
static void findMaxGCD( int arr[], int n)
{
int maxGCD = 0 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
int val = gcd(arr[i], arr[i + 1 ]);
if (val > maxGCD)
{
maxGCD = val;
}
}
System.out.print(maxGCD + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 3 , 18 , 9 , 9 , 5 ,
15 , 8 , 7 , 6 , 9 };
int n = arr.length;
findMaxGCD(arr, n);
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a;
return gcd(b, a % b);
def findMaxGCD(arr, n):
maxGCD = 0 ;
for i in range ( 0 , n - 1 ):
val = gcd(arr[i], arr[i + 1 ]);
if (val > maxGCD):
maxGCD = val;
print (maxGCD);
if __name__ = = '__main__' :
arr = [ 3 , 18 , 9 , 9 , 5 ,
15 , 8 , 7 , 6 , 9 ];
n = len (arr);
findMaxGCD(arr, n);
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
static void findMaxGCD( int []arr, int n)
{
int maxGCD = 0;
for ( int i = 0; i < n - 1; i++)
{
int val = gcd(arr[i], arr[i + 1]);
if (val > maxGCD)
{
maxGCD = val;
}
}
Console.Write(maxGCD + "\n" );
}
public static void Main()
{
int []arr = { 3, 18, 9, 9, 5,
15, 8, 7, 6, 9 };
int n = arr.Length;
findMaxGCD(arr, n);
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
function findMaxGCD(arr, n)
{
let maxGCD = 0;
for (let i = 0; i < n - 1; i++) {
let val = gcd(arr[i], arr[i + 1]);
if (val > maxGCD) {
maxGCD = val;
}
}
document.write(maxGCD + "<br>" );
}
let arr = [ 3, 18, 9, 9, 5,
15, 8, 7, 6, 9 ];
let n = arr.length;
findMaxGCD(arr, n);
</script>
|
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(log(max(a, b)))
Last Updated :
21 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...