给定N个数字的数组arr [] 。任务是找到大小大于1的所有子阵列的最大GCD。
例子:
Input: arr[] = { 3, 18, 9, 9, 5, 15, 8, 7, 6, 9 }
Output: 9
Explanation:
GCD of the subarray {18, 9, 9} is maximum which is 9.
Input: arr[] = { 4, 8, 12, 16, 20, 24 }
Output: 4
Explanation:
GCD of the subarray {4, 18, 12, 16, 20, 24} is maximum which is 4.
天真的方法:想法是生成所有大小大于1的子数组,然后找到所形成的所有子数组的gcd最大值。
时间复杂度: O(N 2 )
高效方法:将两个数字的GCD设为g 。现在,如果我们将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是数组的长度。