📜  所有长度至少为 2 的子数组的最大 GCD

📅  最后修改于: 2021-09-07 04:27:53             🧑  作者: Mango

给定一个由N 个数字组成的数组arr[] 。任务是找到所有大小大于 1 的子数组的最大 GCD。
例子:

朴素的方法:想法是生成所有大小大于1的子数组,然后找到所有子数组的gcd最大值。
时间复杂度: O(N 2 )
有效方法:设两个数的 GCD 为g 。现在,如果我们采取GCD与任何第三号说C,那么,GCD将降低或保持不变,但它永远不会增加。
这个想法是在arr[] 中找到每个连续对的 gcd 并且形成的所有对的 gcd 的最大值是期望的结果。
下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find GCD
int gcd(int a, int b)
{
    if (b == 0) {
        return a;
    }
    return gcd(b, a % b);
}
 
void findMaxGCD(int arr[], int n)
{
 
    // To store the maximum GCD
    int maxGCD = 0;
 
    // Traverse the array
    for (int i = 0; i < n - 1; i++) {
 
        // Find GCD of the consecutive
        // element
        int val = gcd(arr[i], arr[i + 1]);
 
        // If calculated GCD > maxGCD
        // then update it
        if (val > maxGCD) {
            maxGCD = val;
        }
    }
 
    // Print the maximum GCD
    cout << maxGCD << endl;
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 18, 9, 9, 5,
                  15, 8, 7, 6, 9 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    findMaxGCD(arr, n);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find GCD
static int gcd(int a, int b)
{
    if (b == 0)
    {
        return a;
    }
    return gcd(b, a % b);
}
 
static void findMaxGCD(int arr[], int n)
{
 
    // To store the maximum GCD
    int maxGCD = 0;
 
    // Traverse the array
    for(int i = 0; i < n - 1; i++)
    {
         
       // Find GCD of the consecutive
       // element
       int val = gcd(arr[i], arr[i + 1]);
        
       // If calculated GCD > maxGCD
       // then update it
       if (val > maxGCD)
       {
           maxGCD = val;
       }
    }
 
    // Print the maximum GCD
    System.out.print(maxGCD + "\n");
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 3, 18, 9, 9, 5,
                  15, 8, 7, 6, 9 };
    int n = arr.length;
 
    // Function call
    findMaxGCD(arr, n);
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program for the above approach
 
# Function to find GCD
def gcd(a, b):
     
    if (b == 0):
        return a;
    return gcd(b, a % b);
 
def findMaxGCD(arr, n):
     
    # To store the maximum GCD
    maxGCD = 0;
 
    # Traverse the array
    for i in range(0, n - 1):
 
        # Find GCD of the consecutive
        # element
        val = gcd(arr[i], arr[i + 1]);
 
        # If calculated GCD > maxGCD
        # then update it
        if (val > maxGCD):
            maxGCD = val;
 
    # Print the maximum GCD
    print(maxGCD);
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 3, 18, 9, 9, 5,
            15, 8, 7, 6, 9 ];
    n = len(arr);
 
    # Function call
    findMaxGCD(arr, n);
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find GCD
static int gcd(int a, int b)
{
    if (b == 0)
    {
        return a;
    }
    return gcd(b, a % b);
}
 
static void findMaxGCD(int []arr, int n)
{
 
    // To store the maximum GCD
    int maxGCD = 0;
 
    // Traverse the array
    for(int i = 0; i < n - 1; i++)
    {
         
        // Find GCD of the consecutive
        // element
        int val = gcd(arr[i], arr[i + 1]);
             
        // If calculated GCD > maxGCD
        // then update it
        if (val > maxGCD)
        {
            maxGCD = val;
        }
    }
 
    // Print the maximum GCD
    Console.Write(maxGCD + "\n");
}
 
// Driver Code
public static void Main()
{
    int []arr = { 3, 18, 9, 9, 5,
                 15, 8, 7, 6, 9 };
    int n = arr.Length;
 
    // Function call
    findMaxGCD(arr, n);
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:

9

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

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