使所有数组元素相等的最小增量-其他操作数。
我们得到一个由 n 个元素组成的数组。在每个操作中,您可以选择任何一个元素并将其余 n-1 个元素增加 1。您必须使所有元素相等,执行您希望多次的此类操作。找到为此所需的最少操作数。
例子:
Input : arr[] = {1, 2, 3}
Output : Minimum Operation = 3
Explanation :
operation | increased elements | after increment
1 | 1, 2 | 2, 3, 3
2 | 1, 2 | 3, 4, 3
3 | 1, 3 | 4, 4, 4
Input : arr[] = {4, 3, 4}
Output : Minimum Operation = 2
Explanation :
operation | increased elements | after increment
1 | 1, 2 | 5, 4, 4
2 | 2, 3 | 5, 5, 5
蛮力:使所有元素相等的一个简单方法是在每一步找到最大的元素,然后将所有其余的 n-1 个元素加 1。我们应该继续执行此操作,直到所有元素相等。时间复杂度:O(n^2)
更好的方法:如果我们仔细研究每个操作以及问题陈述,我们会发现增加除最大元素之外的所有 n-1 元素类似于仅减少最大元素。因此,最小的元素不再需要减少,其余元素将减少到最小的元素。这样,使所有元素相等所需的操作总数将是arraySum – n * (smallestElement)。时间复杂度将与寻找最小元素和数组总和的时间复杂度相同,即 O(n)。
// find array sum
sum = arraySum (int arr[], int n);
// find the smallest element from array
small = smallest (int arr, int n);
// calculate min operation required
minOperation = sum - (n * small);
// return result
return minOperation;
C++
// CPP for finding minimum operation required
#include
using namespace std;
// function for finding array sum
int arraySum (int arr[], int n)
{
int sum = 0;
for (int i=0; iC++// [STL] - CPP for finding minimum operation required
#include
using namespace std;
// function for finding min operation
int minOp (int arr[], int n)
{
// find array sum
int sum = accumulate(arr,arr+n,0);
// find the smallest element from array
int small = *min_element(arr,arr+n);
// calculate min operation required
int minOperation = sum - (n * small);
// return result
return minOperation;
}
//driver function
int main()
{
int arr[] = {5, 6, 2, 4, 3};
int n = sizeof(arr)/ sizeof(arr[0]);
cout << "Minimum Operation = " << minOp (arr, n);
return 0;
}
// This code is contributed by Shubham Singh
Java
// Java program to find min operation
import java.io.*;
class minOperation
{
/* Function to print minimum operation required
to make all elements of an array equal */
static void printMinOp(int arr[])
{
int arraySum, smallest, arr_size = arr.length;
arraySum = 0;
smallest = arr[0];
for (int i = 0; i < arr_size ; i ++)
{
/* If current element is smaller than
update smallest */
if (arr[i] < smallest)
smallest = arr[i];
/*find array sum */
arraySum += arr[i];
}
int minOperation = arraySum - arr_size * smallest;
/* Print min operation required */
System.out.println("Minimum Operation = " +
minOperation);
}
/* Driver program to test above functions */
public static void main (String[] args)
{
int arr[] = {5, 6, 2, 4, 3};
printMinOp(arr);
}
}
Python3
# Python 3 for finding minimum
# operation required
# function for finding min
# operation
def minOp (arr, n) :
# find array sum
sm = sum(arr)
# find the smallest element from
# array
small = min(arr)
# calculate min operation required
minOperation = sm - (n * small)
# return result
return minOperation
# Driver function
arr = [5, 6, 2, 4, 3]
n = len(arr)
print( "Minimum Operation = ", minOp (arr, n))
# This code is contributed by Shubham Singh
C#
// C# program to find min operation
using System;
class GFG {
/* Function to print minimum
operation required to make all
elements of an array equal */
static void printMinOp(int []arr)
{
int arraySum, smallest,
arr_size = arr.Length;
arraySum = 0;
smallest = arr[0];
for (int i = 0; i < arr_size ; i ++)
{
/* If current element is
smaller than update smallest */
if (arr[i] < smallest)
smallest = arr[i];
/*find array sum */
arraySum += arr[i];
}
int minOperation = arraySum -
arr_size * smallest;
/* Print min operation required */
Console.Write("Minimum Operation = " +
minOperation);
}
/* Driver program to test above
functions */
public static void Main ()
{
int []arr = {5, 6, 2, 4, 3};
printMinOp(arr);
}
}
// This code is contributed by nitin mittal.
PHP
Javascript
输出
Minimum Operation = 10