给定一个由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)