给定一个大小为N > 1的数组arr[] 。任务是通过最多替换一个元素来找到数组的最大可能 GCD。
例子:
Input: arr[] = {6, 7, 8}
Output: 2
Replace 7 with 2 and gcd(6, 2, 8) = 2
which is maximum possible.
Input: arr[] = {12, 18, 30}
Output: 6
方法:
- 想法是找到所有长度为(N – 1)的子序列的 GCD 值,并删除子序列中必须替换的元素,因为它可以被子序列中已有的任何其他元素替换。找到的最大 GCD 就是答案。
- 为了最优地找到子序列的 GCD,使用单状态动态规划维护一个prefixGCD[]和一个suffixGCD[]数组。
- GCD(prefixGCD[i – 1], suffixGCD[i + 1])的最大值就是要求的答案。另请注意,如果必须替换第一个或最后一个元素,还需要比较suffixGCD[1]和prefixGCD[N – 2] 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the maximum
// possible gcd after replacing
// a single element
int MaxGCD(int a[], int n)
{
// Prefix and Suffix arrays
int Prefix[n + 2];
int Suffix[n + 2];
// Single state dynamic programming relation
// for storing gcd of first i elements
// from the left in Prefix[i]
Prefix[1] = a[0];
for (int i = 2; i <= n; i += 1) {
Prefix[i] = __gcd(Prefix[i - 1], a[i - 1]);
}
// Initializing Suffix array
Suffix[n] = a[n - 1];
// Single state dynamic programming relation
// for storing gcd of all the elements having
// index greater than or equal to i in Suffix[i]
for (int i = n - 1; i >= 1; i -= 1) {
Suffix[i] = __gcd(Suffix[i + 1], a[i - 1]);
}
// If first or last element of
// the array has to be replaced
int ans = max(Suffix[2], Prefix[n - 1]);
// If any other element is replaced
for (int i = 2; i < n; i += 1) {
ans = max(ans, __gcd(Prefix[i - 1], Suffix[i + 1]));
}
// Return the maximized gcd
return ans;
}
// Driver code
int main()
{
int a[] = { 6, 7, 8 };
int n = sizeof(a) / sizeof(a[0]);
cout << MaxGCD(a, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the maximum
// possible gcd after replacing
// a single element
static int MaxGCD(int a[], int n)
{
// Prefix and Suffix arrays
int []Prefix = new int[n + 2];
int []Suffix = new int[n + 2];
// Single state dynamic programming relation
// for storing gcd of first i elements
// from the left in Prefix[i]
Prefix[1] = a[0];
for (int i = 2; i <= n; i += 1)
{
Prefix[i] = __gcd(Prefix[i - 1],
a[i - 1]);
}
// Initializing Suffix array
Suffix[n] = a[n - 1];
// Single state dynamic programming relation
// for storing gcd of all the elements having
// index greater than or equal to i in Suffix[i]
for (int i = n - 1; i >= 1; i -= 1)
{
Suffix[i] = __gcd(Suffix[i + 1],
a[i - 1]);
}
// If first or last element of
// the array has to be replaced
int ans = Math.max(Suffix[2], Prefix[n - 1]);
// If any other element is replaced
for (int i = 2; i < n; i += 1)
{
ans = Math.max(ans, __gcd(Prefix[i - 1],
Suffix[i + 1]));
}
// Return the maximized gcd
return ans;
}
static int __gcd(int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
// Driver code
public static void main(String[] args)
{
int a[] = { 6, 7, 8 };
int n = a.length;
System.out.println(MaxGCD(a, n));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 implementation of the approach
from math import gcd as __gcd
# Function to return the maximum
# possible gcd after replacing
# a single element
def MaxGCD(a, n) :
# Prefix and Suffix arrays
Prefix = [0] * (n + 2);
Suffix = [0] * (n + 2);
# Single state dynamic programming relation
# for storing gcd of first i elements
# from the left in Prefix[i]
Prefix[1] = a[0];
for i in range(2, n + 1) :
Prefix[i] = __gcd(Prefix[i - 1], a[i - 1]);
# Initializing Suffix array
Suffix[n] = a[n - 1];
# Single state dynamic programming relation
# for storing gcd of all the elements having
# index greater than or equal to i in Suffix[i]
for i in range(n - 1, 0, -1) :
Suffix[i] = __gcd(Suffix[i + 1], a[i - 1]);
# If first or last element of
# the array has to be replaced
ans = max(Suffix[2], Prefix[n - 1]);
# If any other element is replaced
for i in range(2, n) :
ans = max(ans, __gcd(Prefix[i - 1],
Suffix[i + 1]));
# Return the maximized gcd
return ans;
# Driver code
if __name__ == "__main__" :
a = [ 6, 7, 8 ];
n = len(a);
print(MaxGCD(a, n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the maximum
// possible gcd after replacing
// a single element
static int MaxGCD(int []a, int n)
{
// Prefix and Suffix arrays
int []Prefix = new int[n + 2];
int []Suffix = new int[n + 2];
// Single state dynamic programming relation
// for storing gcd of first i elements
// from the left in Prefix[i]
Prefix[1] = a[0];
for (int i = 2; i <= n; i += 1)
{
Prefix[i] = __gcd(Prefix[i - 1],
a[i - 1]);
}
// Initializing Suffix array
Suffix[n] = a[n - 1];
// Single state dynamic programming relation
// for storing gcd of all the elements having
// index greater than or equal to i in Suffix[i]
for (int i = n - 1; i >= 1; i -= 1)
{
Suffix[i] = __gcd(Suffix[i + 1],
a[i - 1]);
}
// If first or last element of
// the array has to be replaced
int ans = Math.Max(Suffix[2], Prefix[n - 1]);
// If any other element is replaced
for (int i = 2; i < n; i += 1)
{
ans = Math.Max(ans, __gcd(Prefix[i - 1],
Suffix[i + 1]));
}
// Return the maximized gcd
return ans;
}
static int __gcd(int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
// Driver code
public static void Main(String[] args)
{
int []a = { 6, 7, 8 };
int n = a.Length;
Console.WriteLine(MaxGCD(a, n));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
2
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。