Longest sub-array with maximum GCD
Last Updated :
11 Jul, 2022
Given an array arr[] of length N, the task is the find the length of the longest sub-array with the maximum possible GCD value.
Examples:
Input: arr[] = {1, 2, 2}
Output: 2
Here all possible sub-arrays and there GCD’s are:
1) {1} -> 1
2) {2} -> 2
3) {2} -> 2
4) {1, 2} -> 1
5) {2, 2} -> 2
6) {1, 2, 3} -> 1
Here, the maximum GCD value is 2 and longest sub-array having GCD = 2 is {2, 2}.
Thus, the answer is {2, 2}.
Input: arr[] = {3, 3, 3, 3}
Output: 4
Naive approach: Generate all the possible sub-arrays and find the GCD of each of them individually in order to find the longest such sub-array. This approach will take O(N3) time to solve the problem.
Better approach: The maximum GCD value will always be equal to the largest number present in the array. Let’s say that the largest number present in the array is X. Now, the task is to find the largest sub-array having all X. The same can be done using the two-pointer approach. Below is the algorithm:
- Find the largest number in the array. Let us call this number X.
- Run a loop from i = 0
- If arr[i] != X then increment i and continue.
- Else initialize j = i.
- While j < n and arr[j] = X, increment j.
- Update the answer as ans = max(ans, j – i).
- Update i as i = j.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findLength( int * arr, int n)
{
int x = 0;
for ( int i = 0; i < n; i++)
x = max(x, arr[i]);
int ans = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] != x)
continue ;
int j = i;
while (arr[j] == x)
j++;
ans = max(ans, j - i);
}
return ans;
}
int main()
{
int arr[] = { 1, 2, 2 };
int n = sizeof (arr) / sizeof ( int );
cout << findLength(arr, n);
return 0;
}
|
Java
class GFG
{
static int findLength( int []arr, int n)
{
int x = 0 ;
for ( int i = 0 ; i < n; i++)
x = Math.max(x, arr[i]);
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] != x)
continue ;
int j = i;
while (arr[j] == x)
{
j++;
if (j >= n )
break ;
}
ans = Math.max(ans, j - i);
}
return ans;
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 2 };
int n = arr.length;
System.out.println(findLength(arr, n));
}
}
|
Python3
def findLength(arr, n) :
x = 0 ;
for i in range (n) :
x = max (x, arr[i]);
ans = 0 ;
for i in range (n) :
if (arr[i] ! = x) :
continue ;
j = i;
while (arr[j] = = x) :
j + = 1 ;
if j > = n :
break
ans = max (ans, j - i);
return ans;
if __name__ = = "__main__" :
arr = [ 1 , 2 , 2 ];
n = len (arr);
print (findLength(arr, n));
|
C#
using System;
class GFG
{
static int findLength( int []arr, int n)
{
int x = 0;
for ( int i = 0; i < n; i++)
x = Math.Max(x, arr[i]);
int ans = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] != x)
continue ;
int j = i;
while (arr[j] == x)
{
j++;
if (j >= n )
break ;
}
ans = Math.Max(ans, j - i);
}
return ans;
}
public static void Main ()
{
int []arr = { 1, 2, 2 };
int n = arr.Length;
Console.WriteLine(findLength(arr, n));
}
}
|
Javascript
<script>
function findLength(arr, n)
{
var x = 0;
for ( var i = 0; i < n; i++)
x = Math.max(x, arr[i]);
var ans = 0;
for ( var i = 0; i < n; i++) {
if (arr[i] != x)
continue ;
var j = i;
while (arr[j] == x)
j++;
ans = Math.max(ans, j - i);
}
return ans;
}
var arr = [1, 2, 2 ];
var n = arr.length;
document.write( findLength(arr, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...