Length of the Smallest Subarray that must be removed in order to Maximise the GCD
Last Updated :
12 Jul, 2025
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++
// C++ program to find the length of
// the smallest subarray that must be
// removed in order to maximise the GCD
#include <bits/stdc++.h>
using namespace std;
// Function to find the length of
// the smallest subarray that must be
// removed in order to maximise the GCD
int GetMinSubarrayLength(int a[], int n)
{
// Store the maximum possible
// GCD of the resulting subarray
int ans = max(a[0], a[n - 1]);
// Two pointers initially pointing
// to the first and last element
// respectively
int lo = 0, hi = n - 1;
// Moving the left pointer to the
// right if the elements are
// divisible by the maximum GCD
while (lo < n and a[lo] % ans == 0)
lo++;
// Moving the right pointer to the
// left if the elements are
// divisible by the maximum GCD
while (hi > lo and a[hi] % ans == 0)
hi--;
// Return the length of
// the subarray
return (hi - lo + 1);
}
// Driver code
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
// Java program to find the length of
// the smallest subarray that must be
// removed in order to maximise the GCD
class GFG {
// Function to find the length of
// the smallest subarray that must be
// removed in order to maximise the GCD
static int GetMinSubarrayLength(int a[], int n)
{
// Store the maximum possible
// GCD of the resulting subarray
int ans = Math.max(a[0], a[n - 1]);
// Two pointers initially pointing
// to the first and last element
// respectively
int lo = 0, hi = n - 1;
// Moving the left pointer to the
// right if the elements are
// divisible by the maximum GCD
while (lo < n && a[lo] % ans == 0)
lo++;
// Moving the right pointer to the
// left if the elements are
// divisible by the maximum GCD
while (hi > lo && a[hi] % ans == 0)
hi--;
// Return the length of
// the subarray
return (hi - lo + 1);
}
// Driver code
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);
}
}
// This code is contributed by Yash_R
Python3
# Python3 program to find the length of
# the smallest subarray that must be
# removed in order to maximise the GCD
# Function to find the length of
# the smallest subarray that must be
# removed in order to maximise the GCD
def GetMinSubarrayLength(a, n):
# Store the maximum possible
# GCD of the resulting subarray
ans = max(a[0], a[n - 1])
# Two pointers initially pointing
# to the first and last element
# respectively
lo = 0
hi = n - 1
# Moving the left pointer to the
# right if the elements are
# divisible by the maximum GCD
while (lo < n and a[lo] % ans == 0):
lo += 1
# Moving the right pointer to the
# left if the elements are
# divisible by the maximum GCD
while (hi > lo and a[hi] % ans == 0):
hi -= 1
# Return the length of
# the subarray
return (hi - lo + 1)
# Driver code
if __name__ == '__main__':
arr = [4, 8, 2, 1, 4]
N = len(arr)
length = GetMinSubarrayLength(arr, N)
print(length)
# This code is contributed by mohit kumar 29
C#
// C# program to find the length of
// the smallest subarray that must be
// removed in order to maximise the GCD
using System;
class GFG {
// Function to find the length of
// the smallest subarray that must be
// removed in order to maximise the GCD
static int GetMinSubarrayLength(int []a, int n)
{
// Store the maximum possible
// GCD of the resulting subarray
int ans = Math.Max(a[0], a[n - 1]);
// Two pointers initially pointing
// to the first and last element
// respectively
int lo = 0, hi = n - 1;
// Moving the left pointer to the
// right if the elements are
// divisible by the maximum GCD
while (lo < n && a[lo] % ans == 0)
lo++;
// Moving the right pointer to the
// left if the elements are
// divisible by the maximum GCD
while (hi > lo && a[hi] % ans == 0)
hi--;
// Return the length of
// the subarray
return (hi - lo + 1);
}
// Driver code
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);
}
}
// This code is contributed by Yash_R
JavaScript
<script>
// Javascript program to find the length of
// the smallest subarray that must be
// removed in order to maximise the GCD
// Function to find the length of
// the smallest subarray that must be
// removed in order to maximise the GCD
function GetMinSubarrayLength(a, n)
{
// Store the maximum possible
// GCD of the resulting subarray
var ans = Math.max(a[0], a[n - 1]);
// Two pointers initially pointing
// to the first and last element
// respectively
var lo = 0, hi = n - 1;
// Moving the left pointer to the
// right if the elements are
// divisible by the maximum GCD
while (lo < n && a[lo] % ans == 0)
lo++;
// Moving the right pointer to the
// left if the elements are
// divisible by the maximum GCD
while (hi > lo && a[hi] % ans == 0)
hi--;
// Return the length of
// the subarray
return (hi - lo + 1);
}
// Driver code
var arr = [4, 8, 2, 1, 4 ];
var N = arr.length;
var length = GetMinSubarrayLength(arr, N);
document.write( length );
// This code is contributed by itsok.
</script>
Time Complexity: O(N)
Space Complexity: O(1)
Similar Reads
Length of smallest subarray to be removed to make sum of remaining elements divisible by K Given an array arr[] of integers and an integer K, the task is to find the length of the smallest subarray that needs to be removed such that the sum of remaining array elements is divisible by K. Removal of the entire array is not allowed. If it is impossible, then print "-1". Examples: Input: arr[
11 min read
Remove an element to maximize the GCD of the given array Given an array arr[] of length N ⥠2. The task is to remove an element from the given array such that the GCD of the array after removing it is maximized. Examples: Input: arr[] = {12, 15, 18} Output: 6 Remove 12: GCD(15, 18) = 3 Remove 15: GCD(12, 18) = 6 Remove 18: GCD(12, 15) = 3 Input: arr[] = {
13 min read
Remove an element to maximize the GCD of the given array Given an array arr[] of length N ⥠2. The task is to remove an element from the given array such that the GCD of the array after removing it is maximized. Examples: Input: arr[] = {12, 15, 18} Output: 6 Remove 12: GCD(15, 18) = 3 Remove 15: GCD(12, 18) = 6 Remove 18: GCD(12, 15) = 3 Input: arr[] = {
13 min read
Remove an element to maximize the GCD of the given array Given an array arr[] of length N ⥠2. The task is to remove an element from the given array such that the GCD of the array after removing it is maximized. Examples: Input: arr[] = {12, 15, 18} Output: 6 Remove 12: GCD(15, 18) = 3 Remove 15: GCD(12, 18) = 6 Remove 18: GCD(12, 15) = 3 Input: arr[] = {
13 min read
Maximum length subarray with LCM equal to product Given an arr[], the task is to find the maximum length of the sub-array such that the LCM of the sub-array is equal to the product of numbers in the sub-array. If no valid sub-array exists, then print -1. Note: The length of the sub-array must be ? 2. Examples: Input: arr[] = { 6, 10, 21} Output: 2
15+ min read
Subsequence X of length K such that gcd(X[0], X[1]) + (X[2], X[3]) + ... is maximized Given an array a of size N. The task is to find a subsequence X of length K such that gcd(X[0], X[1]) + (X[2], X[3]) + ... is maximum. Note: K is even. Examples: Input: a[] = {4, 5, 3, 7, 8, 10, 9, 8}, k = 4 Output: 9 The subsequence {4, 7, 8, 8} gives the maximum value = 9. Other subsequences like
10 min read