📌  相关文章
📜  为了最大化 GCD 必须移除的最小子阵列的长度

📅  最后修改于: 2021-10-26 06:42:07             🧑  作者: Mango

给定一个包含N 个元素的数组arr[] ,任务是找到最小子数组的长度,使得当从数组中删除这个子数组时,结果数组的 GCD 最大。
注意:生成的数组应该是非空的。
例子:

方法:已知GCD是一个非递增函数。也就是说,如果我们在数组中添加元素,那么 gcd 将减少或保持不变。所以,思路是用这个概念来解决这个问题:

  • 现在我们必须注意,在删除子数组后,生成的子数组应该有第一个或最后一个元素,或者这两个元素都有。这是因为我们需要确保删除子数组后的结果数组应该是非空的。因此,我们无法删除所有元素。
  • 所以最大的 GCD 可能是max(A[0], A[N – 1])
  • 现在我们必须最小化我们需要删除以获得这个答案的子数组的长度。
  • 为此,我们将使用两个指针技术,分别指向第一个和最后一个元素。
  • 现在,如果元素可以被 gcd 整除,我们将增加第一个指针,如果元素可以被 gcd 整除,我们将减少最后一个指针,因为它不会影响我们的答案。
  • 因此,最后,两个指针之间的元素数将是我们需要删除的子数组的长度。

下面是上述方法的实现:

C++
// C++ program to find the length of
// the smallest subarray that must be
// removed in order to maximise the GCD
 
#include 
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


输出:
2

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程