Length of the Smallest Subarray that must be removed in order to Maximise the GCD
Last Updated :
08 May, 2023
Given an array arr[] of N elements, the task is to find the length of the smallest subarray such that when this subarray is removed from the array, the GCD of the resultant array is maximum.
Note: The resulting array should be non-empty.
Examples:
Input: N = 4, arr[] = {3, 6, 1, 2}
Output: 2
Explanation:
If we remove the subarray {1, 2} then the resulting subarray will be {3, 6} and the GCD of this is 3 which is the maximum possible value.
Input: N = 3, arr[] = {4, 8, 4}
Output: 0
Explanation:
Here we don’t need to remove any subarray and the maximum GCD possible is 4.
Approach: It is known that GCD is a non-increasing function. That is, if we add elements in the array, then the gcd will either be decreasing or remain constant. Therefore, the idea is to use this concept to solve this problem:
- Now we have to notice that after removal of a subarray the resulting subarray should have either the first or the last element or both the elements. This is because we need to make sure that the resultant array after removing the subarray should be non-empty. So, we cannot remove all the elements.
- So the maximum GCD possible will be max(A[0], A[N – 1]).
- Now we have to minimize the length of the subarray which we need to remove to obtain this answer.
- To do that, we will use two pointers technique, pointing to the first and the last elements respectively.
- Now we will increase the first pointer if the element is divisible by that gcd and decrease the last pointer if the element is divisible by gcd as it will not affect our answer.
- So, at last, the number of elements between the two pointers will be the length of the subarray which we need to remove.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int GetMinSubarrayLength( int a[], int n)
{
int ans = max(a[0], a[n - 1]);
int lo = 0, hi = n - 1;
while (lo < n and a[lo] % ans == 0)
lo++;
while (hi > lo and a[hi] % ans == 0)
hi--;
return (hi - lo + 1);
}
int main()
{
int arr[] = { 4, 8, 2, 1, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
int length = GetMinSubarrayLength(arr, N);
cout << length << "\n" ;
return 0;
}
|
Java
class GFG {
static int GetMinSubarrayLength( int a[], int n)
{
int ans = Math.max(a[ 0 ], a[n - 1 ]);
int lo = 0 , hi = n - 1 ;
while (lo < n && a[lo] % ans == 0 )
lo++;
while (hi > lo && a[hi] % ans == 0 )
hi--;
return (hi - lo + 1 );
}
public static void main (String[] args)
{
int arr[] = { 4 , 8 , 2 , 1 , 4 };
int N = arr.length;
int Length = GetMinSubarrayLength(arr, N);
System.out.println(Length);
}
}
|
Python3
def GetMinSubarrayLength(a, n):
ans = max (a[ 0 ], a[n - 1 ])
lo = 0
hi = n - 1
while (lo < n and a[lo] % ans = = 0 ):
lo + = 1
while (hi > lo and a[hi] % ans = = 0 ):
hi - = 1
return (hi - lo + 1 )
if __name__ = = '__main__' :
arr = [ 4 , 8 , 2 , 1 , 4 ]
N = len (arr)
length = GetMinSubarrayLength(arr, N)
print (length)
|
C#
using System;
class GFG {
static int GetMinSubarrayLength( int []a, int n)
{
int ans = Math.Max(a[0], a[n - 1]);
int lo = 0, hi = n - 1;
while (lo < n && a[lo] % ans == 0)
lo++;
while (hi > lo && a[hi] % ans == 0)
hi--;
return (hi - lo + 1);
}
public static void Main ( string [] args)
{
int []arr = { 4, 8, 2, 1, 4 };
int N = arr.Length;
int Length = GetMinSubarrayLength(arr, N);
Console.WriteLine(Length);
}
}
|
Javascript
<script>
function GetMinSubarrayLength(a, n)
{
var ans = Math.max(a[0], a[n - 1]);
var lo = 0, hi = n - 1;
while (lo < n && a[lo] % ans == 0)
lo++;
while (hi > lo && a[hi] % ans == 0)
hi--;
return (hi - lo + 1);
}
var arr = [4, 8, 2, 1, 4 ];
var N = arr.length;
var length = GetMinSubarrayLength(arr, N);
document.write( length );
</script>
|
Time Complexity: O(N)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...