Minimize the difference between the maximum and minimum values of the modified array
Last Updated :
07 Sep, 2022
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[] = {1, 3, 6}, x = 3
Output: 0
New array is [3, 3, 3] or [4, 4, 4].
Input: arr[] = {0, 10}, x = 2
Output: 6
New array is [2, 8] i.e add 2 to a[0] and subtract -2 from a[1].
Approach: Let A be the original array. Towards trying to minimize max(A) - min(A), let's try to minimize max(A) and maximize min(A) separately.
The smallest possible value of max(A) is max(A) - K, as the value max(A) cannot go lower. Similarly, the largest possible value of min(A) is min(A) + K. So the quantity max(A) - min(A) is at least ans = (max(A) - K) - (min(A) + K).
We can attain this value, by the following modifications
- If A[i] <= min(A) + K, then A[i] = min(A) + K
- Else, if A[i] >= max(A) - K, then A[i] = max(A) - K
If ans < 0, the best answer we could have is ans = 0, also using the same modification.
Below is the implementation of above approach.
C++
// C++ program to find the minimum difference.
#include <bits/stdc++.h>
using namespace std;
// Function to return required minimum difference
int minDiff(int n, int x, int A[])
{
int mn = A[0], mx = A[0];
// finding minimum and maximum values
for (int i = 0; i < n; ++i) {
mn = min(mn, A[i]);
mx = max(mx, A[i]);
}
// returning minimum possible difference
return max(0, mx - mn - 2 * x);
}
// Driver program
int main()
{
int n = 3, x = 3;
int A[] = { 1, 3, 6 };
// function to return the answer
cout << minDiff(n, x, A);
return 0;
}
Java
// Java program to find the minimum difference.
import java.util.*;
class GFG
{
// Function to return required minimum difference
static int minDiff(int n, int x, int A[])
{
int mn = A[0], mx = A[0];
// finding minimum and maximum values
for (int i = 0; i < n; ++i) {
mn = Math.min(mn, A[i]);
mx = Math.max(mx, A[i]);
}
// returning minimum possible difference
return Math.max(0, mx - mn - 2 * x);
}
// Driver program
public static void main(String []args)
{
int n = 3, x = 3;
int A[] = { 1, 3, 6 };
// function to return the answer
System.out.println(minDiff(n, x, A));
}
}
// This code is contributed by ihritik
Python3
# Python program to find the minimum difference.
# Function to return required minimum difference
def minDiff( n, x, A):
mn = A[0]
mx = A[0]
# finding minimum and maximum values
for i in range(0,n):
mn = min( mn, A[ i])
mx = max( mx, A[ i])
# returning minimum possible difference
return max(0, mx - mn - 2 * x)
# Driver program
n = 3
x = 3
A = [1, 3, 6 ]
# function to return the answer
print(minDiff( n, x, A))
# This code is contributed by ihritik
C#
// C# program to find the minimum difference.
using System;
class GFG
{
// Function to return required minimum difference
static int minDiff(int n, int x, int []A)
{
int mn = A[0], mx = A[0];
// finding minimum and maximum values
for (int i = 0; i < n; ++i) {
mn = Math.Min(mn, A[i]);
mx = Math.Max(mx, A[i]);
}
// returning minimum possible difference
return Math.Max(0, mx - mn - 2 * x);
}
// Driver program
public static void Main()
{
int n = 3, x = 3;
int []A = { 1, 3, 6 };
// function to return the answer
Console.WriteLine(minDiff(n, x, A));
}
}
// This code is contributed by ihritik
PHP
<?php
// PHP program to find the minimum difference.
// Function to return required minimum difference
function minDiff($n, $x, $A)
{
$mn = $A[0];
$mx = $A[0];
// finding minimum and maximum values
for ($i = 0; $i < $n; ++$i) {
$mn = min($mn, $A[$i]);
$mx = max($mx, $A[$i]);
}
// returning minimum possible difference
return max(0, $mx - $mn - 2 * $x);
}
// Driver program
$n = 3;
$x = 3;
$A = array( 1, 3, 6 );
// function to return the answer
echo minDiff($n, $x, $A);
// This code is contributed by ihritik
?>
JavaScript
<script>
// JavaScript program to find the minimum difference.
// Function to return required minimum difference
function minDiff( n, x, A)
{
var mn = A[0], mx = A[0];
// finding minimum and maximum values
for (var i = 0; i < n; ++i) {
mn = Math.min(mn, A[i]);
mx = Math.max(mx, A[i]);
}
// returning minimum possible difference
return Math.max(0, mx - mn - 2 * x);
}
var n = 3, x = 3;
var A = [ 1, 3, 6 ];
// function to return the answer
document.write( minDiff(n, x, A));
// This code is contributed by SoumikMondal
</script>
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Similar Reads
Minimize the maximum value of the absolute difference Given array A[] of size N (1 <= N <= 105). The following operation should be performed until array A[] is not empty. choose three integers X, Y, and Z, Take out an element from array A[] (1 <= A[i] <= 109), and choose one integer out of the chosen three integers X, Y, and Z record absolu
10 min read
Minimize the difference between minimum and maximum elements 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 = 2Output : Max heigh
8 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
Split a given array into K subarrays minimizing the difference between their maximum and minimum Given a sorted array arr[] of N integers and an integer K, the task is to split the array into K subarrays such that the sum of the difference of maximum and minimum element of each subarray is minimized. Examples: Input: arr[] = {1, 3, 3, 7}, K = 4 Output: 0 Explanation: The given array can be spli
6 min read
Find the maximum possible value of the minimum value of modified array Given an array of size N and a number S . The task is to modify the given array such that: The difference between the sum of the array elements before and after modification is exactly equal to S.Modified array elements should be non-negative.The minimum value in the modified array must be maximized
9 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