给定长度为N的数组arr [] ,任务是找到长度最大为2的最长子数组的长度,并具有最大可能的GCD值。
例子:
Input: arr[] = {1, 2, 2}
Output: 2
Explanation:
Possible sub-arrays of size greater than 2 and there GCD’s are:
1) {1, 2} -> 1
2) {2, 2} -> 2
3) {1, 2, 3} -> 1
Here, the maximum GCD value is 2 and longest sub-array having GCD = 2 is {2, 2}.
Hence the answer is {2, 2}.
Input: arr[] = {18, 3, 6, 9}
Output: 4
Explanation:
Here, the maximum GCD value is 3 and longest sub-array having GCD = 3 is {18, 3, 6, 9}.
Hence the answer is {18, 3, 6, 9}.
天真的方法:这个想法是生成大小至少为2的所有可能的子数组,并分别找到它们的每个子数组的GCD。然后,具有最大GCD值的子数组的长度就是所需的长度。
时间复杂度: O(N 2 )
高效方法:
- 通过使用本文讨论的方法,找到长度至少为2的所有子阵列的最大GCD(例如g )。
- 遍历给定的数组并计算可被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是数组的长度。