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).
- In one operation choose 2 different index, i and j and decrement arr[i] and increment arr[j] by 1.
- Notice this operation can be performed any number of times.
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:
- Create a variable sum = 0.
- Run a loop from 0 to (N-1) and make sum += arr[index].
- 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++
#include <bits/stdc++.h>
using namespace std;
int minimizeDifference( int arr[], int N)
{
int sum = 0;
for ( int index = 0; index < N; index++)
sum += arr[index];
if (sum % N == 0) {
return 0;
}
else {
return 1;
}
}
int main()
{
int N = 6;
int arr[] = { 1, 2, 3, 4, 4, 1 };
cout << minimizeDifference(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int minimizeDifference( int [] arr, int N)
{
int sum = 0 ;
for ( int index = 0 ; index < N; index++)
sum += arr[index];
if (sum % N == 0 ) {
return 0 ;
}
else {
return 1 ;
}
}
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));
}
}
|
Python3
def minimizeDifference(arr, N):
sum = 0
for index in range (N):
sum + = arr[index]
if ( sum % N = = 0 ):
return 0
else :
return 1
N = 6
arr = [ 1 , 2 , 3 , 4 , 4 , 1 ]
print (minimizeDifference(arr, N))
|
C#
using System;
class GFG
{
static int minimizeDifference( int []arr, int N)
{
int sum = 0;
for ( int index = 0; index < N; index++)
sum += arr[index];
if (sum % N == 0) {
return 0;
}
else {
return 1;
}
}
public static void Main()
{
int N = 6;
int []arr = { 1, 2, 3, 4, 4, 1 };
Console.Write(minimizeDifference(arr, N));
}
}
|
Javascript
<script>
const minimizeDifference = (arr, N) => {
let sum = 0;
for (let index = 0; index < N; index++)
sum += arr[index];
if (sum % N == 0) {
return 0;
}
else {
return 1;
}
}
let N = 6;
let arr = [1, 2, 3, 4, 4, 1];
document.write(minimizeDifference(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)