📌  相关文章
📜  通过从它们的最大值中重复减去对的绝对差,使所有数组元素相等

📅  最后修改于: 2021-10-26 06:48:49             🧑  作者: Mango

给定一个N 个整数组成的数组arr[] ,任务是通过从数组中选择任意一对整数并用它们的绝对差替换任意次数的绝对差来使所有数组元素相等。打印所有数组元素的最终值。

例子:

处理方法:从上面的问题陈述可以看出,对于任何一对(a, b) ,从最大元素中减去绝对差。那么这个操作类似于寻找对的GCD。因此,从这个观察中,很明显,所有数组元素都需要减少到数组的GCD。请按照以下步骤解决问题:

  • 将变量gcd初始化为1
  • 遍历给定数组并在遍历时将gcd更新为:
  • 在上述步骤之后, gcd的值是在给定的操作应用于每个不同的元素对之后所需的数组元素。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to return
// gcd of a and b
int gcd(int a, int b)
{
  // Base Case
  if (a == 0)
    return b;
 
  // Recursive Call
  return gcd(b % a, a);
}
 
// Function to find gcd of array
int findGCD(int arr[], int N)
{
  // Initialise the result
  int result = 0;
 
  // Traverse the array arr[]
  for (int i = 0; i < N; i++)
  {
    // Update result as gcd of
    // the result and arr[i]
    result = gcd(result, arr[i]);
 
    if (result == 1)
    {
      return 1;
    }
  }
 
  // Return the resultant GCD
    return result;
}
 
// Driver Code
int main()
{
  // Given array arr[]
  int arr[] = {2, 3, 4};
 
  int N = sizeof(arr) /
          sizeof(arr[0]);
 
  // Function Call
  cout << findGCD(arr, N);
  return 0;
}
 
// This code is contributed by 29AjayKumar


Java
// Java program for the above approach
 
public class GCD {
 
    // Function to return gcd of a and b
    static int gcd(int a, int b)
    {
        // Base Case
        if (a == 0)
            return b;
 
        // Recursive Call
        return gcd(b % a, a);
    }
 
    // Function to find gcd of array
    static int findGCD(int arr[], int N)
    {
        // Initialise the result
        int result = 0;
 
        // Traverse the array arr[]
        for (int element : arr) {
 
            // Update result as gcd of
            // the result and arr[i]
            result = gcd(result, element);
 
            if (result == 1) {
                return 1;
            }
        }
 
        // Return the resultant GCD
        return result;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given array arr[]
        int arr[] = { 2, 3, 4 };
 
        int N = arr.length;
 
        // Function Call
        System.out.println(findGCD(arr, N));
    }
}


Python3
# Python3 program for the above approach
 
# Function to return gcd of a and b
def gcd(a, b):
     
    # Base Case
    if (a == 0):
        return b
 
    # Recursive call
    return gcd(b % a, a)
 
# Function to find gcd of array
def findGCD(arr, N):
     
    # Initialise the result
    result = 0
 
    # Traverse the array arr[]
    for element in arr:
 
        # Update result as gcd of
        # the result and arr[i]
        result = gcd(result, element)
 
        if (result == 1):
            return 1
 
    # Return the resultant GCD
    return result
 
# Driver Code
 
# Given array arr[]
arr = [ 2, 3, 4 ]
 
N = len(arr)
 
# Function call
print(findGCD(arr, N))
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to return gcd of a and b
static int gcd(int a, int b)
{
     
    // Base Case
    if (a == 0)
        return b;
 
    // Recursive call
    return gcd(b % a, a);
}
 
// Function to find gcd of array
static int findGCD(int[] arr, int N)
{
     
    // Initialise the result
    int result = 0;
 
    // Traverse the array arr[]
    foreach(int element in arr)
    {
 
        // Update result as gcd of
        // the result and arr[i]
        result = gcd(result, element);
 
        if (result == 1)
        {
            return 1;
        }
    }
 
    // Return the resultant GCD
    return result;
}
 
// Driver Code
public static void Main()
{
     
    // Given array arr[]
    int[] arr = { 2, 3, 4 };
 
    int N = arr.Length;
 
    // Function call
    Console.WriteLine(findGCD(arr, N));
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出
1

时间复杂度: O(N*logN),其中 N 是给定数组的大小。
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程