Minimize difference between maximum and minimum array elements by exactly K removals
Last Updated :
18 May, 2021
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 = 3
Output: 2
Explanation:
Remove elements 12, 10 and 1 from the given array.
The array modifies to {5, 6, 7}.
The difference between the minimum and maximum element is 7 – 5 = 2.
Input: arr[] = {14, 5, 61, 10, 21, 12, 54}, K = 4
Output: 4
Explanation:
Remove elements 61, 54, 5 and 21 from the given array.
The array modifies to {14, 10, 12}.
The difference between the minimum and maximum element is 14 – 10 = 4.
Approach: The idea to solve the given problem is that the difference will be minimized by removing either the minimum element in an array or the maximum element in the array. Follow the steps below to solve the problem:
- Sort the array arr[] in ascending order.
- Initialize variables left = 0 and right = (N – 1).
- Iterate for K times and change the maximum or minimum to 0 according to the following condition:
- If arr[right – 1] – arr[left] < arr[right] – arr[left + 1], then change arr[right] to 0 and decrement right pointer by 1.
- Else, change arr[left] as 0 and increment the left pointer by 1.
- After the above steps, the difference between the elements at the left and right index is the required minimum difference.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void minimumRange( int arr[], int N, int K)
{
if (K >= N)
{
cout << 0;
return ;
}
sort(arr, arr + N);
int left = 0, right = N - 1, i;
for (i = 0; i < K; i++)
{
if (arr[right - 1] - arr[left]
< arr[right] - arr[left + 1])
right--;
else
left++;
}
cout << arr[right] - arr[left];
}
int main()
{
int arr[] = { 5, 10, 12, 14, 21, 54, 61 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 4;
minimumRange(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minimumRange( int arr[], int N,
int K)
{
if (K >= N)
{
System.out.print( 0 );
return ;
}
Arrays.sort(arr);
int left = 0 , right = N - 1 , i;
for (i = 0 ; i < K; i++)
{
if (arr[right - 1 ] - arr[left] <
arr[right] - arr[left + 1 ])
right--;
else
left++;
}
System.out.print(arr[right] - arr[left]);
}
public static void main(String[] args)
{
int arr[] = { 5 , 10 , 12 , 14 , 21 , 54 , 61 };
int N = arr.length;
int K = 4 ;
minimumRange(arr, N, K);
}
}
|
Python3
def minimumRange(arr, N, K) :
if (K > = N) :
print ( 0 , end = '');
return ;
arr.sort();
left = 0 ; right = N - 1 ;
for i in range (K) :
if (arr[right - 1 ] - arr[left] < arr[right] - arr[left + 1 ]) :
right - = 1 ;
else :
left + = 1 ;
print (arr[right] - arr[left], end = '');
if __name__ = = "__main__" :
arr = [ 5 , 10 , 12 , 14 , 21 , 54 , 61 ];
N = len (arr);
K = 4 ;
minimumRange(arr, N, K);
|
C#
using System;
class GFG{
static void minimumRange( int []arr, int N,
int K)
{
if (K >= N)
{
Console.Write(0);
return ;
}
Array.Sort(arr);
int left = 0, right = N - 1, i;
for (i = 0; i < K; i++)
{
if (arr[right - 1] - arr[left] <
arr[right] - arr[left + 1])
right--;
else
left++;
}
Console.Write(arr[right] - arr[left]);
}
public static void Main(String[] args)
{
int []arr = { 5, 10, 12, 14, 21, 54, 61 };
int N = arr.Length;
int K = 4;
minimumRange(arr, N, K);
}
}
|
Javascript
<script>
function minimumRange(arr, N, K)
{
if (K >= N)
{
document.write( 0);
return ;
}
arr.sort((a,b)=> a-b);
var left = 0, right = N - 1, i;
for (i = 0; i < K; i++)
{
if (arr[right - 1] - arr[left]
< arr[right] - arr[left + 1])
right--;
else
left++;
}
document.write( arr[right] - arr[left]);
}
var arr = [5, 10, 12, 14, 21, 54, 61];
var N = arr.length;
var K = 4;
minimumRange(arr, N, K);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...