📜  长度至少为 2 且具有最大 GCD 的最长子阵列的长度

📅  最后修改于: 2021-09-06 05:11:24             🧑  作者: Mango

给定一个长度为N的数组arr[] ,任务是找到长度至少为 2 且具有最大可能 GCD 值的最长子数组的长度。
例子:

朴素的方法:这个想法是生成大小至少为 2 的所有可能子数组,并分别找到它们每个子数组的 GCD。那么,GCD 值最大的子数组的长度就是需要的长度。
时间复杂度: O(N 2 )
有效的方法:

  1. 使用本文中讨论的方法找到长度至少为 2 的所有子数组的最大 GCD(比如g )。
  2. 遍历给定数组并计算可被GCD g整除的最大连续元素数。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate GCD of two numbers
int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
 
// Function to find maximum size subarray
// having maximum GCD
int maximumGcdSubarray(int arr[], int n)
{
    // Base Case
    if (n == 1)
        return 0;
 
    // Let the maximum GCD be 1 initially
    int k = 1;
 
    // Loop thourgh array to find maximum
    // GCD of subarray with size 2
    for (int i = 1; i < n; ++i) {
        k = max(k, gcd(arr[i], arr[i - 1]));
    }
 
    int cnt = 0;
    int maxLength = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // Is a multiple of k, increase cnt
        if (arr[i] % k == 0) {
            cnt++;
        }
 
        // Else update maximum length with
        // consecutive element divisible by k
        // Set cnt to 0
        else {
            maxLength = max(maxLength, cnt);
            cnt = 0;
        }
    }
 
    // Update the maxLength
    maxLength = max(maxLength, cnt);
 
    // Return the maxLength
    return maxLength;
}
 
// Driver Code
int main()
{
    int arr[] = { 18, 3, 6, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << maximumGcdSubarray(arr, n);
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to calculate GCD of
// two numbers
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
     
// Function to find maximum size 
// subarray having maximum GCD
static int maximumGcdSubarray(int arr[], int n)
{
     
    // Base case
    if (n == 1)
        return 0;
         
    // Let the maximum GCD be 1 initially
    int k = 1;
         
    // Loop through array to find maximum
    // GCD of subarray with size 2
    for(int i = 1; i < n; i++)
    {
       k = Math.max(k, gcd(arr[i], arr[i - 1]));
    }
         
    int cnt = 0;
    int maxLength = 0;
         
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
        
       // Is a multiple of k, increase cnt
       if(arr[i] % k == 0)
       {
           cnt++;
       }
        
       // Else update maximum length with
       // consecutive element divisible by k
       // Set cnt to 0
       else
       {
           maxLength = Math.max(maxLength, cnt);
           cnt = 0;
       }
    }
     
    // Update the maxLength
    maxLength = Math.max(maxLength, cnt);
         
    // Return the maxLength
    return maxLength;
}
     
// Driver Code
public static void main(String args[])
{
    int arr[] = { 18, 3, 6, 9 };
    int n = arr.length;
         
    // Function call
    System.out.println(maximumGcdSubarray(arr, n));
}
}
 
// This code is contributed by stutipathak31jan


Python3
# Python3 program for the above approach
 
# Function to calculate GCD of
# two numbers
def gcd(a, b):
     
    if b == 0:
        return a
    return gcd(b, a % b)
 
# Function to find maximum size
# subarray having maximum GCD
def maxGcdSubarray(arr, n):
     
    # Base case
    if n == 1:
        return 0
     
    # Let the maximum GCD be 1 initially
    k = 1
     
    # Loop through array to find maximum
    # GCD of subarray with size 2
    for i in range(1, n):
        k = max(k, gcd(arr[i], arr[i - 1]))
         
    cnt = 0
    maxLength = 0
     
    # Traverse the array
    for i in range(n):
         
        # Is a multiple of k, increase cnt
        if arr[i] % k == 0:
            cnt += 1
         
        # Else update maximum length with
        # consecutive element divisible by k
        # Set cnt to 0
        else:
            maxLength = max(maxLength, cnt)
            cnt = 0
             
    # Update the maxLength
    maxLength = max(maxLength, cnt)
     
    # Return the maxLength
    return maxLength
 
# Driver Code
arr = [ 18, 3, 6, 9 ]
n = len(arr)
     
# Function call
print(maxGcdSubarray(arr, n))
 
# This code is contributed by stutipathak31jan


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to calculate GCD of
// two numbers
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
     
// Function to find maximum size
// subarray having maximum GCD
static int maximumGcdSubarray(int[] arr, int n)
{
     
    // Base case
    if (n == 1)
        return 0;
         
    // Let the maximum GCD be 1 initially
    int k = 1;
         
    // Loop through array to find maximum
    // GCD of subarray with size 2
    for(int i = 1; i < n; i++)
    {
        k = Math.Max(k, gcd(arr[i], arr[i - 1]));
    }
         
    int cnt = 0;
    int maxLength = 0;
         
    // Traverse the array
    for(int i = 0; i < n; i++)
    {
         
        // Is a multiple of k, increase cnt
        if(arr[i] % k == 0)
        {
            cnt++;
        }
             
        // Else update maximum length with
        // consecutive element divisible by k
        // Set cnt to 0
        else
        {
            maxLength = Math.Max(maxLength, cnt);
            cnt = 0;
        }
    }
     
    // Update the maxLength
    maxLength = Math.Max(maxLength, cnt);
         
    // Return the maxLength
    return maxLength;
}
 
// Driver code   
static void Main()
{
    int[] arr = { 18, 3, 6, 9 };
    int n = arr.Length;
         
    // Function call
    Console.WriteLine(maximumGcdSubarray(arr, n));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
4

时间复杂度: O(N) ,其中 N 是数组的长度。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live