Minimize difference between maximum and minimum element by decreasing and increasing Array elements by 1
Last Updated :
09 Feb, 2022
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++
// 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
// 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
<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>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimize difference between maximum and minimum array elements by removing a K-length subarray Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum difference between the maximum and minimum element present in the array after removing any subarray of size K. Examples: Input: arr[] = {4, 5, 8, 9, 1, 2}, K = 2Output: 4Explanation: Remove the subarray {
10 min read
Minimize sum of absolute difference between all pairs of array elements by decrementing and incrementing pairs by 1 Given an array arr[] ( 1-based indexing ) consisting of N integers, the task is to find the minimum sum of the absolute difference between all pairs of array elements by decrementing and incrementing any pair of elements by 1 any number of times. Examples: Input: arr[] = {1, 2, 3}Output: 0Explanatio
5 min read
Minimize difference between maximum and minimum array elements by exactly K removals Given an array arr[] consisting of N positive integers and an integer K, the task is to minimize the difference between the maximum and minimum element in the given array after removing exactly K elements. Examples: Input: arr[] = {5, 1, 6, 7, 12, 10}, K = 3Output: 2Explanation:Remove elements 12, 1
6 min read
Minimum count of array elements that must be changed such that difference between maximum and minimum array element is N - 1 Given an array arr[] consisting of N integers, the task is to find the minimum number of array elements that must be changed to any arbitrary integers such that the difference between maximum and minimum array element is (N - 1) and all array elements must be distinct. Examples: Input: arr[] = {1, 2
7 min read
Minimize absolute difference between the smallest and largest array elements by minimum increment decrement operations Given an array arr[] consisting of N positive integers, the task is to minimize the number of operations required to minimize the absolute difference between the smallest and largest elements present in the array. In each operation, subtract 1 from an array element and increment 1 to another array e
8 min read