给定一个由N 个整数组成的数组arr[] ,任务是通过从数组中选择任意一对整数并用它们的绝对差替换任意次数的绝对差来使所有数组元素相等。打印所有数组元素的最终值。
例子:
Input: arr[] ={2, 3, 4}
Output: 1
Explanation:
Step 1: Performing on the pair (2, 3) modifies arr[] = {2, 1, 4}
Step 2: Performing on the pair (2, 4) modifies arr[] = {2, 1, 2}
Step 3: Performing on the pair (2, 1) modifies {1, 1, 2}
Step 4: Performing on the pair (1, 2) modifies arr[] = {1, 1, 1}
Input: arr[] = {24, 60}
Output: 12
处理方法:从上面的问题陈述可以看出,对于任何一对(a, b) ,从最大元素中减去绝对差。那么这个操作类似于寻找对的GCD。因此,从这个观察中,很明显,所有数组元素都需要减少到数组的GCD。请按照以下步骤解决问题:
- 将变量gcd初始化为1 。
- 遍历给定数组并在遍历时将gcd更新为:
gcd = gcd(arr[i], gcd), where 0 ≤ i < N
- 在上述步骤之后, 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 现场工作专业课程和学生竞争性编程现场课程。