📌  相关文章
📜  D 的最小可能值,当它与 K 相加或相减时,重复获得每个数组元素

📅  最后修改于: 2021-10-26 05:37:00             🧑  作者: Mango

给定一个大小为N的数组arr[]和一个整数K ,任务是找到D的最大可能值这样每个数组元素都可以从K的初始值开始通过将K更改为K – DK + D在每一步。

例子:

方法:这个问题可以通过找到最大公约数来解决 每个数组元素与K之间的绝对差异按照以下步骤解决问题

  • 遍历数组arr[],将当前元素arr[i]的值改为abs(arr[i] – K)
  • 初始化一个变量,比如Darr[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)