最大化可以从 Array 中减少的 K 以使所有元素相等
给定一个大小为N的数组arr[] ,任务是通过对给定数组中的任何元素应用下面给出的任意次数(可能为零)的操作来使所有元素相等。
- 选择数组中的一个元素。
- 将其减少一个正整数K 。
在所有这些正k 中,打印最大的K。
例子:
Input: arr[] = {3, 7, 5, 3, 3, 7}
Output: 2
Explanation: Choose K = 2, decrement both 7s twice and one 5 once. to get all the elements equal to 3
Input: arr[] = {100, -2000, -2000, -2000}
Output: 2100
Input: arr[] = {2, 2, 2}
Output: -1
Explanation: As all the elements are already equal hence there can be infinite number of such K possible.
方法:可以在一些观察的基础上解决该任务。可以使所有数组元素等于数组的最小元素。最大 K 可以通过按排序顺序找到相邻元素的最大公约数来获得。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum value of K
int maxValue(int arr[], int N)
{
// Sorting array of integers
sort(arr, arr + N);
// Initializing a variable
int ans = 0;
// Iterating using a for loop
for (int i = 1; i < N; i++) {
// Find the gcd of ans and
// (arr[i] - arr[i - 1])
ans = __gcd(ans, arr[i] - arr[i - 1]);
}
// Return the answer
return ans;
}
// Driver code
int main()
{
// Initializing an array of integers
int arr[] = { 3, 7, 5, 3, 3, 7 };
// Number of elements in the array
int N = sizeof(arr) / sizeof(int);
int ans = maxValue(arr, N);
if (ans > 0)
cout << ans;
else
cout << "-1";
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
import java.math.BigInteger;
class GFG {
//calculate gcd
static int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
// Function to find the maximum value of K
static int maxValue(int arr[], int N)
{
// Sorting array of integers
Arrays.sort(arr);
// Initializing a variable
int ans = 0;
// Iterating using a for loop
for (int i = 1; i < N; i++) {
// Find the gcd of ans and
// (arr[i] - arr[i - 1])
ans = gcd(ans, arr[i] - arr[i - 1]);
}
// Return the answer
return ans;
}
// Driver code
public static void main (String[] args) {
int arr[] = { 3, 7, 5, 3, 3, 7 };
// Number of elements in the array
int N = arr.length;
int ans = maxValue(arr, N);
if (ans > 0)
System.out.println(ans);
else
System.out.println("-1");
}
}
// This code is contributed by hrithikgarg03188.
Python3
# Python program for the above approach
# calculate gcd
def gcd(a, b):
return a if b == 0 else gcd(b, a % b);
# Function to find the maximum value of K
def maxValue(arr, N):
# Sorting array of integers
arr.sort();
# Initializing a variable
ans = 0;
# Iterating using a for loop
for i in range(1,N):
# Find the gcd of ans and
# (arr[i] - arr[i - 1])
ans = gcd(ans, arr[i] - arr[i - 1]);
# Return the answer
return ans;
# Driver code
if __name__ == '__main__':
arr = [3, 7, 5, 3, 3, 7];
# Number of elements in the array
N = len(arr);
ans = maxValue(arr, N);
if (ans > 0):
print(ans);
else:
print("-1");
# This code is contributed by shikhasingrajput
C#
// C# program for the above approach
using System;
class GFG {
// JavaScript code for the above approach
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
// Function to find the maximum value of K
static int maxValue(int[] arr, int N)
{
// Sorting array of integers
Array.Sort(arr);
// Initializing a variable
int ans = 0;
// Iterating using a for loop
for (int i = 1; i < N; i++) {
// Find the gcd of ans and
// (arr[i] - arr[i - 1])
ans = __gcd(ans, arr[i] - arr[i - 1]);
}
// Return the answer
return ans;
}
public static void Main()
{
// Initializing an array of integers
int[] arr = { 3, 7, 5, 3, 3, 7 };
// Number of elements in the array
int N = arr.Length;
int ans = maxValue(arr, N);
if (ans > 0)
Console.Write(ans);
else
Console.Write(-1);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
输出
2
时间复杂度:O(N * logN)
辅助空间:O(1)