在 Array 中完成的最小增量使得 arr[i] 可以使所有其他元素相等
给定一个大小为N的数组arr[] ,任务是找到数组中元素所需的最小增量,使得如果任何元素说arr[i]分布在其他元素中,则使N – 1 个元素相等。
例子:
Input: arr[] = {0, 0, 3}, N = 3
Output: 3
Explanation: Increments can be done in the following way:
- Increment element at index 0 by 1, so arr[] becomes {1, 0, 3}
- Increment element at index 1 by 2. so arr[] becomes {1, 2, 3}
Now, if any element is chosen and distributed in others, makes all N – 1 elements equal.
Lets say,
- choose 1, add it to 2, so N – 1 elements become {3, 3}
- choose 2, add it to 1, so N – 1 elements become {3, 3}
- choose 3, add 2 of 3 to 1 and add 1 of 3 to 2, so N – 1 elements become {3, 3}
So, total increments = (1 + 2) = 3
Input: arr[] = {4, 3, 1, 6}, N = 4
Output: 4
方法:解决这个问题的想法是基于以下观察:如果(N-1)*mx其中mx是数组的最大元素大于数组的总和,那么答案就是(N-1) *mx - 总和。否则,如果(N-1)*mx小于或等于 sum 然后取一个临时变量,比如temp用sum / N – 1分配它。如果sum mod N -1不等于0 ,则将 temp 增加1并取一个计数器来表示count ,其中count是 temp 和mx的差。更新ans = (count + mx) * (N – 1)并按 count 递增 sum。如果sum等于(N-1)*mx返回count ,否则返回 count 以(N-1)*mx的差值递增和总和。请按照以下步骤解决问题:
- 将变量mx和sum初始化为0以找到数组的总和和最大元素。
- 使用变量i遍历范围[0, N)并执行以下任务:
- 将mx的值更新为mx或arr[i]的最大值,并将arr [i] 的值添加到变量sum。
- 将变量ans初始化为(N-1)*mx。
- 如果ans大于sum则返回ans-sum作为答案。
- 否则,将变量temp初始化为sum/(N-1) + sum%(N-1)。
- 将变量count初始化为temp – mx 。
- 将ans的值设置为(count + mx)*(N-1)。
- 将count的值添加到变量sum 中。
- 如果sum等于ans,则返回count的值作为答案,否则返回count + ans – sum。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find minimum increment
// required to an element to make N-1
// elements equal by distributing
// any element
int minIncrement(int arr[], int N)
{
// Variable for max element
// and sum
int mx = 0, sum = 0;
for (int i = 0; i < N; i++) {
mx = max(mx, arr[i]);
sum += arr[i];
}
// Calculate ans
int ans = (N - 1) * mx;
// If ans is greater than sum
// return its difference
if (ans > sum) {
return (ans - sum);
}
// If ans is less or equal to sum
else {
int temp = sum / (N - 1);
if (sum % (N - 1) != 0) {
temp++;
}
int count = temp - mx;
ans = (count + mx) * (N - 1);
sum += count;
// If sum is equal to ans
// return the count
if (sum == ans) {
return count;
}
// Else return the summation
// of count and difference
// of ans and sum
else {
return (count + (ans - sum));
}
}
}
// Driver Code
int main()
{
int arr[] = { 4, 3, 1, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << minIncrement(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find minimum increment
// required to an element to make N-1
// elements equal by distributing
// any element
static int minIncrement(int arr[], int N)
{
// Variable for max element
// and sum
int mx = 0, sum = 0;
for (int i = 0; i < N; i++) {
mx = Math.max(mx, arr[i]);
sum += arr[i];
}
// Calculate ans
int ans = (N - 1) * mx;
// If ans is greater than sum
// return its difference
if (ans > sum) {
return (ans - sum);
}
// If ans is less or equal to sum
else {
int temp = sum / (N - 1);
if (sum % (N - 1) != 0) {
temp++;
}
int count = temp - mx;
ans = (count + mx) * (N - 1);
sum += count;
// If sum is equal to ans
// return the count
if (sum == ans) {
return count;
}
// Else return the summation
// of count and difference
// of ans and sum
else {
return (count + (ans - sum));
}
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 4, 3, 1, 6 };
int N = arr.length;
System.out.print(minIncrement(arr, N));
}
}
// This code is contributed by 29AjayKumar
Python3
# python program for the above approach
# Function to find minimum increment
# required to an element to make N-1
# elements equal by distributing
# any element
def minIncrement(arr, N):
# Variable for max element
# and sum
mx = 0
sum = 0
for i in range(0, N):
mx = max(mx, arr[i])
sum += arr[i]
# Calculate ans
ans = (N - 1) * mx
# If ans is greater than sum
# return its difference
if (ans > sum):
return (ans - sum)
# If ans is less or equal to sum
else:
temp = sum // (N - 1)
if (sum % (N - 1) != 0):
temp += 1
count = temp - mx
ans = (count + mx) * (N - 1)
sum += count
# If sum is equal to ans
# return the count
if (sum == ans):
return count
# Else return the summation
# of count and difference
# of ans and sum
else:
return (count + (ans - sum))
# Driver Code
if __name__ == "__main__":
arr = [4, 3, 1, 6]
N = len(arr)
print(minIncrement(arr, N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
class GFG {
// Function to find minimum increment
// required to an element to make N-1
// elements equal by distributing
// any element
static int minIncrement(int []arr, int N)
{
// Variable for max element
// and sum
int mx = 0, sum = 0;
for (int i = 0; i < N; i++) {
mx = Math.Max(mx, arr[i]);
sum += arr[i];
}
// Calculate ans
int ans = (N - 1) * mx;
// If ans is greater than sum
// return its difference
if (ans > sum) {
return (ans - sum);
}
// If ans is less or equal to sum
else {
int temp = sum / (N - 1);
if (sum % (N - 1) != 0) {
temp++;
}
int count = temp - mx;
ans = (count + mx) * (N - 1);
sum += count;
// If sum is equal to ans
// return the count
if (sum == ans) {
return count;
}
// Else return the summation
// of count and difference
// of ans and sum
else {
return (count + (ans - sum));
}
}
}
// Driver Code
public static void Main()
{
int []arr = { 4, 3, 1, 6 };
int N = arr.Length;
Console.Write(minIncrement(arr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
4
时间复杂度: O(N)
辅助空间: O(1)