Minimize the difference between minimum and maximum elements
Last Updated :
05 May, 2025
Given an array of N integers and an integer k . It is allowed to modify an element either by increasing or decreasing them by k (only once).
The task is to minimize and print the maximum difference between the shortest and longest towers.
Examples:
Input: arr[] = {1, 10, 8, 5}, k = 2
Output : Max height difference = 5
Explanation:
modified arr[]={3, 8, 6, 7}
max difference: 5
Input: arr[] = {3, 16, 12, 9, 20}, k = 3
Output : Max height difference: 11
Approach:
- Find the max and min elements present in the array.
- Check whether the difference between the max and min element is less than or equal to k or not:
- If yes, then return the difference between the max and min element.
- otherwise, go to step 3.
- Calculate the average of the max and min elements of the array.
- Traverse the array and do the following operations:
- If the array element is greater than the average then decrease it by k.
- If the array element is smaller than the average then increase it by k.
- Return the difference between the max and min elements of the modified array.
Below is the implementation of above approach:
C++
// C++ program to minimize the difference between
// minimum and maximum elements
#include <bits/stdc++.h>
using namespace std;
// Function to minimize the difference between
// minimum and maximum elements
int minimizeDiff(int* arr, int n, int k)
{
// Find max and min elements of the array
int max = *(max_element(arr, arr + n));
int min = *(min_element(arr, arr + n));
// Check whether the difference between
// the max and min element is less than
// or equal to k or not
if ((max - min) <= k) {
return (max - min);
}
// Calculate average of max and min
int avg = (max + min) / 2;
for (int i = 0; i < n; i++) {
// If the array element is greater than the
// average then decrease it by k
if (arr[i] > avg)
arr[i] -= k;
// If the array element is smaller than the
// average then increase it by k
else
arr[i] += k;
}
// Find max and min of the modified array
max = *(max_element(arr, arr + n));
min = *(min_element(arr, arr + n));
// return the new difference
return (max - min);
}
// Driver code
int main()
{
int arr[] = { 3, 16, 12, 9, 20 };
int n = 5;
int k = 3;
cout << "Max height difference = "
<< minimizeDiff(arr, n, k) << endl;
return 0;
}
Java
// Java program to minimize the difference between
// minimum and maximum elements
import java.util.*;
class GFG
{
// Function to minimize the difference between
// minimum and maximum elements
static int minimizeDiff(int[] arr, int n, int k)
{
// Find max and min elements of the array
int max = Arrays.stream(arr).max().getAsInt();
int min = Arrays.stream(arr).min().getAsInt();
// Check whether the difference between
// the max and min element is less than
// or equal to k or not
if ((max - min) <= k)
{
return (max - min);
}
// Calculate average of max and min
int avg = (max + min) / 2;
for (int i = 0; i < n; i++)
{
// If the array element is greater than the
// average then decrease it by k
if (arr[i] > avg)
{
arr[i] -= k;
}
// If the array element is smaller than the
// average then increase it by k
else
{
arr[i] += k;
}
}
// Find max and min of the modified array
max = Arrays.stream(arr).max().getAsInt();
min = Arrays.stream(arr).min().getAsInt();
// return the new difference
return (max - min);
}
// Driver code
public static void main(String[] args)
{
int arr[] = {3, 16, 12, 9, 20};
int n = 5;
int k = 3;
System.out.println("Max height difference = "
+ minimizeDiff(arr, n, k));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python 3 program to minimize the
# difference between minimum and
# maximum elements
# Function to minimize the difference
# between minimum and maximum elements
def minimizeDiff(arr, n, k) :
# Find max and min elements
# of the array
max_element = max(arr)
min_element = min(arr)
# Check whether the difference between
# the max and min element is less than
# or equal to k or not
if ((max_element - min_element) <= k) :
return (max_element - min_element)
# Calculate average of max and min
avg = (max_element + min_element) // 2
for i in range(n):
# If the array element is greater than
# the average then decrease it by k
if (arr[i] > avg) :
arr[i] -= k
# If the array element is smaller than
# the average then increase it by k
else :
arr[i] += k
# Find max and min of the
# modified array
max_element = max(arr)
min_element = min(arr)
# return the new difference
return (max_element - min_element);
# Driver code
if __name__ == "__main__" :
arr = [ 3, 16, 12, 9, 20 ]
n = 5
k = 3
print("Max height difference =",
minimizeDiff(arr, n, k))
# This code is contributed by Ryuga
C#
// C# program to minimize the difference between
// minimum and maximum elements
using System;
using System.Linq;
class GFG
{
// Function to minimize the difference between
// minimum and maximum elements
static int minimizeDiff(int[] arr, int n, int k)
{
// Find max and min elements of the array
int max = arr.Max();
int min = arr.Min();
// Check whether the difference between
// the max and min element is less than
// or equal to k or not
if ((max - min) <= k)
{
return (max - min);
}
// Calculate average of max and min
int avg = (max + min) / 2;
for (int i = 0; i < n; i++)
{
// If the array element is greater than the
// average then decrease it by k
if (arr[i] > avg)
{
arr[i] -= k;
}
// If the array element is smaller than the
// average then increase it by k
else
{
arr[i] += k;
}
}
// Find max and min of the modified array
max = arr.Max();
min = arr.Min();
// return the new difference
return (max - min);
}
// Driver code
public static void Main()
{
int []arr = {3, 16, 12, 9, 20};
int n = 5;
int k = 3;
Console.WriteLine("Max height difference = "
+ minimizeDiff(arr, n, k));
}
}
/* This code contributed by PrinciRaj1992 */
JavaScript
<script>
// Javascript program to minimize
// the difference between
// minimum and maximum elements
// Function to minimize the difference between
// minimum and maximum elements
function minimizeDiff(arr,n,k)
{
// Find max and min elements of the array
let max = Math.max(...arr);
let min = Math.min(...arr);
// Check whether the difference between
// the max and min element is less than
// or equal to k or not
if ((max - min) <= k)
{
return (max - min);
}
// Calculate average of max and min
let avg = Math.floor((max + min) / 2);
for (let i = 0; i < n; i++)
{
// If the array element is greater than the
// average then decrease it by k
if (arr[i] > avg)
{
arr[i] -= k;
}
// If the array element is smaller than the
// average then increase it by k
else
{
arr[i] += k;
}
}
// Find max and min of the modified array
max = Math.max(...arr);
min = Math.min(...arr);
// return the new difference
return (max - min);
}
// Driver code
let arr=[3, 16, 12, 9, 20];
let n = 5;
let k = 3;
document.write("Max height difference = "
+ minimizeDiff(arr, n, k));
// This code is contributed by avanitrachhadiya2155
</script>
PHP
<?php
// PHP program to minimize the difference
// between minimum and maximum elements
// Function to minimize the difference
// between minimum and maximum elements
function minimizeDiff(&$arr, $n, $k)
{
// Find max and min elements
// of the array
$max = max($arr);
$min = min($arr);
// Check whether the difference between
// the max and min element is less than
// or equal to k or not
if (($max - $min) <= $k)
{
return ($max - $min);
}
// Calculate average of max and min
$avg = ($max + $min) / 2;
for ($i = 0; $i < $n; $i++)
{
// If the array element is greater than
// the average then decrease it by k
if ($arr[$i] > $avg)
$arr[$i] -= $k;
// If the array element is smaller than
// the average then increase it by k
else
$arr[$i] += $k;
}
// Find max and min of the
// modified array
$max = max($arr);
$min = min($arr);
// return the new difference
return ($max - $min);
}
// Driver code
$arr = array( 3, 16, 12, 9, 20 );
$n = 5;
$k = 3;
echo "Max height difference = " .
minimizeDiff($arr, $n, $k). "\n";
// This code is contributed by ita_c
?>
OutputMax height difference = 11
Time Complexity: O( N )
Space Complexity: O( 1 )
Similar Reads
Minimize difference between maximum and minimum element of all possible subarrays Given an array arr[ ] of size N, the task is to find the minimum difference between maximum and minimum elements of all possible sized subarrays of arr[ ]. Examples: Input: arr[] = { 5, 14, 7, 10 } Output: 3Explanation: {7, 10} is the subarray having max element = 10 & min element = 7, and their
5 min read
Min difference between maximum and minimum element in all Y size subarrays Given an array arr[] of size N and integer Y, the task is to find a minimum of all the differences between the maximum and minimum elements in all the sub-arrays of size Y. Examples: Input: arr[] = { 3, 2, 4, 5, 6, 1, 9 } Y = 3Output: 2Explanation:All subarrays of length = 3 are:{3, 2, 4} where maxi
15+ min read
Minimum distance between the maximum and minimum element of a given Array Given an array A[] consisting of N elements, the task is to find the minimum distance between the minimum and the maximum element of the array.Examples: Input: arr[] = {3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 8, 2} Output: 3 Explanation: The minimum element(= 1) is present at indices {2, 4} The maximum elemen
8 min read
Minimize the difference between the maximum and minimum values of the modified array Given an array A of n integers and integer X. You may choose any integer between -X\leq k\leq X , and add k to A[i] for each 0\leq i \leq n-1 . The task is to find the smallest possible difference between the maximum value of A and the minimum value of A after updating array A. Examples: Input: arr[
5 min read
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). In one operation choose 2 different index, i and j and decrement arr[i] and
5 min read