通过将 Array 元素减少和增加 1 来最小化最大和最小元素之间的差异
给定一个数组arr[] ,由N个正整数组成。任务是通过执行以下操作任意次数(可能为零)来最小化数组的最大元素和最小元素之间的差异。
- 在一次操作中,选择 2 个不同的索引i和j并递减arr[i] 并将 arr[j]递增1。
- 请注意,此操作可以执行任意次数。
例子:
Input: arr[] = {1, 1, 1}
Output: 0
Explanation: Since, all the numbers are equal, so there is no need to perform any operation.
The minimum difference would be 0.
Input: arr[] = {2, 2, 3, 1}
Output: 0
Explanation: Take i = 2 and j = 3, arr[2] = 3 – 1 = 2, arr[3] = 1 + 1 = 2.
The array becomes, [2, 2, 2, 2]. The difference = 0.
Input: arr[] = {1, 2, 3, 4, 4, 1}
Output: 1
Explanation: In this case, 2 operations can be performed making the final array
arr[] = [2, 2, 3, 3, 3, 2].The difference would be 1.
Notice this is the minimum difference produces it can’t be less than 1.
- Take i = 1, j = 3, array becomes, [2, 2, 3, 3, 4, 1]
- Take i = 5, j = 4, array becomes, [2, 2, 3, 3, 3, 2]
方法:解决方案基于贪婪方法。如果最大和最小元素之间的差大于1 ,则可以分别对最大和最小元素应用该操作,从而使它们彼此更接近。这证实了答案总是小于或等于 1。如果数组元素的总和可被数组的大小整除,则答案为 0,否则为 1。请按照以下步骤解决问题:
- 创建一个变量sum = 0 。
- 运行从0到(N-1)的循环并使sum += arr[index] 。
- 检查总和是否可以被N整除,如果为真,则打印0否则打印1 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum difference
// between maximum and minimum elements
int minimizeDifference(int arr[], int N)
{
// Variable to store sum of elements
int sum = 0;
// Run a loop and update the sum
for (int index = 0; index < N; index++)
sum += arr[index];
// If divisible by N print 0
if (sum % N == 0) {
return 0;
}
else {
return 1;
}
}
// Driver Code
int main()
{
int N = 6;
int arr[] = { 1, 2, 3, 4, 4, 1 };
cout << minimizeDifference(arr, N);
return 0;
}
Java
// JAVA program for the above approach
import java.util.*;
class GFG
{
// Function to find the minimum difference
// between maximum and minimum elements
public static int minimizeDifference(int[] arr, int N)
{
// Variable to store sum of elements
int sum = 0;
// Run a loop and update the sum
for (int index = 0; index < N; index++)
sum += arr[index];
// If divisible by N print 0
if (sum % N == 0) {
return 0;
}
else {
return 1;
}
}
// Driver Code
public static void main(String[] args)
{
int N = 6;
int[] arr = new int[] { 1, 2, 3, 4, 4, 1 };
System.out.print(minimizeDifference(arr, N));
}
}
// This code is contributed by Taranpreet
Python3
# Python program for the above approach
# Function to find the minimum difference
# between maximum and minimum elements
def minimizeDifference(arr, N):
# Variable to store sum of elements
sum = 0
# Run a loop and update the sum
for index in range(N):
sum += arr[index]
# If divisible by N print 0
if (sum % N == 0):
return 0
else:
return 1
# Driver Code
N = 6
arr = [1, 2, 3, 4, 4, 1]
print(minimizeDifference(arr, N))
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the minimum difference
// between maximum and minimum elements
static int minimizeDifference(int []arr, int N)
{
// Variable to store sum of elements
int sum = 0;
// Run a loop and update the sum
for (int index = 0; index < N; index++)
sum += arr[index];
// If divisible by N print 0
if (sum % N == 0) {
return 0;
}
else {
return 1;
}
}
// Driver Code
public static void Main()
{
int N = 6;
int []arr = { 1, 2, 3, 4, 4, 1 };
Console.Write(minimizeDifference(arr, N));
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
1
时间复杂度:O(N)
辅助空间:O(1)