给定一个大小为N的数组arr[]和一个整数K ,任务是找到D的最大可能值,这样每个数组元素都可以从K的初始值开始,通过将K更改为K – D或K + D在每一步。
例子:
Input: arr[ ] = {1, 7, 11}, K = 3
Output: 2
Explanation:
Considering the value of D to be 2, every array element can be obtained by the following operations:
- arr[0](= 1): Decrementing 2 from K(=3) obtains arr[0].
- arr[1](= 7): Incrementing K(=3) by 2 times D obtains arr[1].
- arr[2](= 11): Incrementing K(=3) by 4 times D obtains arr[2].
Therefore, D (=2) satisfies the conditions. Also, it is the maximum possible value of D.
Input: arr[ ] = {33, 105, 57}, K = 81
Output: 24
方法:这个问题可以通过找到最大公约数来解决 每个数组元素与K之间的绝对差异。按照以下步骤解决问题
- 遍历数组arr[],将当前元素arr[i]的值改为abs(arr[i] – K) 。
- 初始化一个变量,比如D为arr[0],以存储结果。
- 使用变量在[1, N – 1]范围内迭代,例如i,并且在每次迭代中,将D的值更新为gcd (D, arr[i])。
- 完成以上步骤后,打印D的值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Recursive function tox
// previous gcd of a and b
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the maximum value
// of D such that every element in
// the array can be obtained by
// performing K + D or K - D
int findMaxD(int arr[], int N, int K)
{
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// Update arr[i]
arr[i] = abs(arr[i] - K);
}
// Stores GCD of the array
int D = arr[0];
// Iterate over the range [1, N]
for (int i = 1; i < N; i++) {
// Update the value of D
D = gcd(D, arr[i]);
}
// Print the value of D
return D;
}
// Driver Code
int main()
{
int arr[] = { 1, 7, 11 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 3;
cout << findMaxD(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Recursive function tox
// previous gcd of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the maximum value
// of D such that every element in
// the array can be obtained by
// performing K + D or K - D
static int findMaxD(int arr[], int N, int K)
{
// Traverse the array arr[]
for(int i = 0; i < N; i++)
{
// Update arr[i]
arr[i] = Math.abs(arr[i] - K);
}
// Stores GCD of the array
int D = arr[0];
// Iterate over the range [1, N]
for(int i = 1; i < N; i++)
{
// Update the value of D
D = gcd(D, arr[i]);
}
// Print the value of D
return D;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 7, 11 };
int N = arr.length;
int K = 3;
System.out.print(findMaxD(arr, N, K));
}
}
// This code is contributed by rishavmahato348
Python3
# // python program for the above approach
# // Recursive function tox
# // previous gcd of a and b
def gcd(a, b):
if (b == 0):
return a
return gcd(b, a % b)
# // Function to find the maximum value
# // of D such that every element in
# // the array can be obtained by
# // performing K + D or K - D
def findMaxD(arr, N, K):
# // Traverse the array arr[]
for i in range(0, N):
# // Update arr[i]
arr[i] = abs(arr[i] - K)
# // Stores GCD of the array
D = arr[0]
# // Iterate over the range[1, N]
for i in range(1, N):
# // Update the value of D
D = gcd(D, arr[i])
# // Print the value of D
return D
# // Driver Code
arr = [1, 7, 11]
N = len(arr)
K = 3
print(findMaxD(arr, N, K))
# This code is contributed by amreshkumar3
C#
// C# program for the above approach
using System;
class GFG {
// Recursive function tox
// previous gcd of a and b
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the maximum value
// of D such that every element in
// the array can be obtained by
// performing K + D or K - D
static int findMaxD(int[] arr, int N, int K)
{
// Traverse the array arr[]
for (int i = 0; i < N; i++) {
// Update arr[i]
arr[i] = Math.Abs(arr[i] - K);
}
// Stores GCD of the array
int D = arr[0];
// Iterate over the range [1, N]
for (int i = 1; i < N; i++) {
// Update the value of D
D = gcd(D, arr[i]);
}
// Print the value of D
return D;
}
// Driver Code
public static void Main()
{
int[] arr = { 1, 7, 11 };
int N = arr.Length;
int K = 3;
Console.Write(findMaxD(arr, N, K));
}
}
// This code is contributed by subhammahato348.
Javascript
输出
2
时间复杂度: O(N*log(N))
辅助空间: O(1)