📌  相关文章
📜  通过将 Array 元素减少和增加 1 来最小化最大和最小元素之间的差异

📅  最后修改于: 2022-05-13 01:56:09.326000             🧑  作者: Mango

通过将 Array 元素减少和增加 1 来最小化最大和最小元素之间的差异

给定一个数组arr[] ,由N个正整数组成。任务是通过执行以下操作任意次数(可能为零)来最小化数组的最大元素和最小元素之间的差异

  • 在一次操作中,选择 2 个不同的索引ij递减arr[i] 并将 arr[j]递增1。
  • 请注意,此操作可以执行任意次数。

例子:

方法:解决方案基于贪婪方法。如果最大最小元素之间的差大于1 ,则可以分别对最大最小元素应用该操作,从而使它们彼此更接近。这证实了答案总是小于或等于 1。如果数组元素的总和可被数组的大小整除,则答案为 0,否则为 1。请按照以下步骤解决问题:

  1. 创建一个变量sum = 0
  2. 运行从0(N-1)的循环并使sum += arr[index]
  3. 检查总和是否可以被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)