Length of longest subarray of length at least 2 with maximum GCD
Last Updated :
08 Mar, 2023
Given an array arr[] of length N, the task is the find the length of longest subarray of length at least 2 with maximum possible GCD value.
Examples:
Input: arr[] = {1, 2, 2}
Output: 2
Explanation:
Possible sub-arrays of size greater than 2 and there GCD’s are:
1) {1, 2} -> 1
2) {2, 2} -> 2
3) {1, 2, 3} -> 1
Here, the maximum GCD value is 2 and longest sub-array having GCD = 2 is {2, 2}.
Hence the answer is {2, 2}.
Input: arr[] = {18, 3, 6, 9}
Output: 4
Explanation:
Here, the maximum GCD value is 3 and longest sub-array having GCD = 3 is {18, 3, 6, 9}.
Hence the answer is {18, 3, 6, 9}.
Naive Approach: The idea is to generate all the possible subarray of size at least 2 and find the GCD of each subarray of them individually. Then, the length of the subarray with maximum GCD value is the required length.
Time Complexity: O(N2)
Space Complexity: O(1)
Efficient Approach:
- Find the maximum GCD(say g) of all the subarray with length atleast 2 by using the approach discussed in this article.
- Traverse the given array and count the maximum number of consecutive elements which are divisible by GCD g.
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);
}
int maximumGcdSubarray( int arr[], int n)
{
if (n == 1)
return 0;
int k = 1;
for ( int i = 1; i < n; ++i) {
k = max(k, gcd(arr[i], arr[i - 1]));
}
int cnt = 0;
int maxLength = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % k == 0) {
cnt++;
}
else {
maxLength = max(maxLength, cnt);
cnt = 0;
}
}
maxLength = max(maxLength, cnt);
return maxLength;
}
int main()
{
int arr[] = { 18, 3, 6, 9 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << maximumGcdSubarray(arr, n);
return 0;
}
|
Java
class GFG{
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int maximumGcdSubarray( int arr[], int n)
{
if (n == 1 )
return 0 ;
int k = 1 ;
for ( int i = 1 ; i < n; i++)
{
k = Math.max(k, gcd(arr[i], arr[i - 1 ]));
}
int cnt = 0 ;
int maxLength = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] % k == 0 )
{
cnt++;
}
else
{
maxLength = Math.max(maxLength, cnt);
cnt = 0 ;
}
}
maxLength = Math.max(maxLength, cnt);
return maxLength;
}
public static void main(String args[])
{
int arr[] = { 18 , 3 , 6 , 9 };
int n = arr.length;
System.out.println(maximumGcdSubarray(arr, n));
}
}
|
Python3
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def maxGcdSubarray(arr, n):
if n = = 1 :
return 0
k = 1
for i in range ( 1 , n):
k = max (k, gcd(arr[i], arr[i - 1 ]))
cnt = 0
maxLength = 0
for i in range (n):
if arr[i] % k = = 0 :
cnt + = 1
else :
maxLength = max (maxLength, cnt)
cnt = 0
maxLength = max (maxLength, cnt)
return maxLength
arr = [ 18 , 3 , 6 , 9 ]
n = len (arr)
print (maxGcdSubarray(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int maximumGcdSubarray( int [] arr, int n)
{
if (n == 1)
return 0;
int k = 1;
for ( int i = 1; i < n; i++)
{
k = Math.Max(k, gcd(arr[i], arr[i - 1]));
}
int cnt = 0;
int maxLength = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] % k == 0)
{
cnt++;
}
else
{
maxLength = Math.Max(maxLength, cnt);
cnt = 0;
}
}
maxLength = Math.Max(maxLength, cnt);
return maxLength;
}
static void Main()
{
int [] arr = { 18, 3, 6, 9 };
int n = arr.Length;
Console.WriteLine(maximumGcdSubarray(arr, n));
}
}
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function maximumGcdSubarray(arr, n)
{
if (n == 1)
return 0;
let k = 1;
for (let i = 1; i < n; ++i) {
k = Math.max(k, gcd(arr[i], arr[i - 1]));
}
let cnt = 0;
let maxLength = 0;
for (let i = 0; i < n; i++) {
if (arr[i] % k == 0) {
cnt++;
}
else {
maxLength = Math.max(maxLength, cnt);
cnt = 0;
}
}
maxLength = Math.max(maxLength, cnt);
return maxLength;
}
let arr = [ 18, 3, 6, 9 ];
let n = arr.length;
document.write(maximumGcdSubarray(arr, n));
</script>
|
Time Complexity: O(N), where N is the length of the array.
Auxiliary Space: O(log(max(a, b)))
Share your thoughts in the comments
Please Login to comment...