Length of the Smallest Subarray that must be removed in order to Maximise the GCD

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:

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
chevron_right

Output:
2

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, Yash_R

Article Tags :