找到 K 使得从 Array 元素中重复减去 K 使得 Array 相等
给定一个大小为N的数组arr[] ,任务是找到一个整数K的值,使得它从数组元素中重复减去将使所有数组元素都处于最小操作中。
例子:
Input: arr[] = {5, 3, 3, 7}
Output: 2
Explanation: Minimum 2 operations must be performed:
1st operation: subtract 2 from elements at indices {0, 3}, arr[] = {3, 3, 3, 5}
2nd operation: subtract 2 from element at index 3, arr[] = {3, 3, 3, 3}
So, the value of K = 2
Input: arr[] = {-1, 0, -1, 0, -1}
Output: 1
方法:要使数组arr中的所有元素相等,需要将所有元素更改为数组中的最小值。因此,所有数组元素与最小元素之差的 gcd 将是K的值。现在,要解决以下问题,请按照以下步骤操作:
- 创建一个变量mn来存储数组arr中的最小元素。
- 现在,遍历数组arr并找到所有数组元素与mn之间差异的 gcd。将此值存储在变量K中。
- 返回K ,作为这个问题的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the value to be
// subtracted from array elements
// to make all elements equal
int findtheValue(int arr[], int N)
{
// Minimum element in the array
int mn = *min_element(arr, arr + N);
int K = arr[0] - mn;
// Traverse the array to find the gcd
// of the differences between
// all array elements and mn
for (int i = 1; i < N; ++i) {
K = __gcd(K, arr[i] - mn);
}
return K;
}
// Driver Code
int main()
{
int arr[] = { 5, 3, 3, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << findtheValue(arr, N);
return 0;
}
Java
// Java program for the above approach
class GFG {
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int getMin(int[] inputArray){
int minValue = inputArray[0];
for(int i = 1; i < inputArray.length; i++){
if(inputArray[i] < minValue){
minValue = inputArray[i];
}
}
return minValue;
}
// Function to find the value to be
// subtracted from array elements
// to make all elements equal
static int findtheValue(int[] arr, int N)
{
// Minimum element in the array
int mn = getMin(arr);
int K = arr[0] - mn;
// Traverse the array to find the gcd
// of the differences between
// all array elements and mn
for (int i = 1; i < N; ++i) {
K = gcd(K, arr[i] - mn);
}
return K;
}
// Driver Code
public static void main(String args[])
{
int[] arr = { 5, 3, 3, 7 };
int N = arr.length;
System.out.println(findtheValue(arr, N));
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# python program for the above approach
import math
# Function to find the value to be
# subtracted from array elements
# to make all elements equal
def findtheValue(arr, N):
# Minimum element in the array
mn = min(arr)
K = arr[0] - mn
# Traverse the array to find the gcd
# of the differences between
# all array elements and mn
for i in range(1, N):
K = math.gcd(K, arr[i] - mn)
return K
# Driver Code
if __name__ == "__main__":
arr = [5, 3, 3, 7]
N = len(arr)
print(findtheValue(arr, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Linq;
class GFG {
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Function to find the value to be
// subtracted from array elements
// to make all elements equal
static int findtheValue(int[] arr, int N)
{
// Minimum element in the array
int mn = arr.Min();
int K = arr[0] - mn;
// Traverse the array to find the gcd
// of the differences between
// all array elements and mn
for (int i = 1; i < N; ++i) {
K = gcd(K, arr[i] - mn);
}
return K;
}
// Driver Code
public static void Main()
{
int[] arr = { 5, 3, 3, 7 };
int N = arr.Length;
Console.WriteLine(findtheValue(arr, N));
}
}
// This code is contributed by ukasp.
Javascript
输出
2
时间复杂度: O(Nlog(mn))
辅助空间: O(1)