当每个元素被其余数替换为 K 时,最大化 K 以生成给定的数组 Palindrome
给定一个数组 A[]包含N个正整数,任务是找到可能的最大数K ,使得在将所有元素替换为模K(A[i]=A[i]%K, for all 0<=i< N),数组变成回文。如果K无限大,则打印 -1。
例子:
Input: A={1, 2, 3, 4}, N=4
Output:
1
Explanation:
For K=1, A becomes {1%1, 2%1, 3%1, 4%1}={0, 0, 0, 0} which is a palindromic array.
Input: A={1, 2, 3, 2, 1}, N=5
Output:
-1
观察:以下观察有助于解决问题:
- 如果数组已经是回文数, K可以无限大。
- 两个数,比如A和B ,可以通过取它们的模数和它们的差(|AB|)以及它们的差的因素来相等。
方法:问题可以通过使K等于GCD来解决 A[i]和A[Ni-1] 的绝对差值。请按照以下步骤解决问题:
- 检查A是否已经是回文。如果是,则返回-1 。
- 将数组的第一个和最后一个元素的绝对差存储在一个变量中,比如K ,它将存储将A变为回文所需的最大数。
- 从1 遍历到 N/2-1 ,对于每个当前索引 i,执行以下操作:
- 用GCD更新K K和A[i]和A[Ni-1] 的绝对差。
- 返回K 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// utility function to calculate the GCD of two numbers
int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// Function to calculate the largest K, replacing all
// elements of an array A by their modulus with K, makes A a
// palindromic array
int largestK(int A[], int N)
{
// check if A is palindrome
int l = 0, r = N - 1, flag = 0;
while (l < r) {
// A is not palindromic
if (A[l] != A[r]) {
flag = 1;
break;
}
l++;
r--;
}
// K can be infitely large in this case
if (flag == 0)
return -1;
// variable to store the largest K that makes A
// palindromic
int K = abs(A[0] - A[N - 1]);
for (int i = 1; i < N / 2; i++)
K = gcd(K, abs(A[i] - A[N - i - 1]));
// return the required answer
return K;
}
// Driver code
int main()
{
// Input
int A[] = { 1, 2, 3, 2, 1 };
int N = sizeof(A) / sizeof(A[0]);
// Function call
cout << largestK(A, N) << endl;
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Utility function to calculate the GCD
// of two numbers
static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// Function to calculate the largest K,
// replacing all elements of an array A
// by their modulus with K, makes A a
// palindromic array
static int largestK(int A[], int N)
{
// Check if A is palindrome
int l = 0, r = N - 1, flag = 0;
while (l < r)
{
// A is not palindromic
if (A[l] != A[r])
{
flag = 1;
break;
}
l++;
r--;
}
// K can be infitely large in this case
if (flag == 0)
return -1;
// Variable to store the largest K
// that makes A palindromic
int K = Math.abs(A[0] - A[N - 1]);
for(int i = 1; i < N / 2; i++)
K = gcd(K, Math.abs(A[i] - A[N - i - 1]));
// Return the required answer
return K;
}
// Driver code
public static void main(String[] args)
{
// Input
int A[] = { 1, 2, 3, 2, 1 };
int N = A.length;
// Function call
System.out.println(largestK(A, N));
}
}
// This code is contributed by sanjoy_62
Python3
# Python3 program for the above approach
# utility function to calculate the GCD of two numbers
def gcd(a, b):
if (b == 0):
return a
else:
return gcd(b, a % b)
# Function to calculate the largest K, replacing all
# elements of an array A by their modulus with K, makes A a
# palindromic array
def largestK(A, N):
# check if A is palindrome
l,r,flag = 0, N - 1, 0
while (l < r):
# A is not palindromic
if (A[l] != A[r]):
flag = 1
break
l += 1
r -= 1
# K can be infitely large in this case
if (flag == 0):
return -1
# variable to store the largest K that makes A
# palindromic
K = abs(A[0] - A[N - 1])
for i in range(1,N//2):
K = gcd(K, abs(A[i] - A[N - i - 1]))
# return the required answer
return K
# Driver code
if __name__ == '__main__':
# Input
A= [1, 2, 3, 2, 1 ]
N = len(A)
# Function call
print (largestK(A, N))
# This code is contributed by mohit kumar 29.
C#
// c# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// utility function to calculate the GCD of two numbers
static int gcd(int a, int b)
{
if (b == 0)
return a;
else
return gcd(b, a % b);
}
// Function to calculate the largest K, replacing all
// elements of an array A by their modulus with K, makes A a
// palindromic array
static int largestK(int []A, int N)
{
// check if A is palindrome
int l = 0, r = N - 1, flag = 0;
while (l < r) {
// A is not palindromic
if (A[l] != A[r]) {
flag = 1;
break;
}
l++;
r--;
}
// K can be infitely large in this case
if (flag == 0)
return -1;
// variable to store the largest K that makes A
// palindromic
int K = Math.Abs(A[0] - A[N - 1]);
for (int i = 1; i < N / 2; i++)
K = gcd(K, Math.Abs(A[i] - A[N - i - 1]));
// return the required answer
return K;
}
// Driver code
public static void Main()
{
// Input
int []A = { 1, 2, 3, 2, 1 };
int N = A.Length;
// Function call
Console.Write(largestK(A, N));
}
}
// This code is contributed by ipg2016107.
Javascript
输出
-1
时间复杂度: O(NLogM),其中 M 是数组中的最大元素
辅助空间: O(1)