Open In App

Minimize difference between maximum and minimum element by decreasing and increasing Array elements by 1

Given an array arr[], consisting of N positive integers. The task is to minimize the difference between the maximum and the minimum element of the array by performing the below operation any number of times (possibly zero). 
 

Examples:



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]
 

Approach: The solution is based on greedy approach. If the difference between the maximum and the minimum element is greater than 1, then the operation can be applied to the maximum and the minimum element respectively, which brings them nearer to each other. This confirms that the answer would always be less than or equal to 1. Now the answer is 0 if the sum of the elements of the array is divisible by the size of the array else it’s 1. Follow the steps to solve the problem:

  1. Create a variable sum = 0.
  2. Run a loop from 0 to (N-1) and make sum += arr[index].
  3. Check if the sum is divisible by N or not if true then print 0 else print 1.

Below is the implementation of the above approach:






// C++ program for the above approach
#include <bits/stdc++.h>
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 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




# 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# 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.




<script>
    // JavaScript program for the above approach
 
    // Function to find the minimum difference
    // between maximum and minimum elements
    const minimizeDifference = (arr, N) => {
        // Variable to store sum of elements
        let sum = 0;
 
        // Run a loop and update the sum
        for (let 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
 
    let N = 6;
    let arr = [1, 2, 3, 4, 4, 1];
    document.write(minimizeDifference(arr, N));
 
// This code is contributed by rakeshsahni
 
</script>

 
 

Output
1

 

Time Complexity: O(N)
Auxiliary Space: O(1)

 


Article Tags :