Minimize difference between maximum and minimum of Array by at most K replacements
Given an array arr[] and an integer K, that task is to choose at most K elements of the array and replace it by any number. Find the minimum difference between the maximum and minimum value of the array after performing at most K replacement.
Examples:
Input: arr[] = {1, 4, 6, 11, 15}, k = 3
Output: 2
Explanation:
k = 1, arr = {4, 4, 6, 11, 15}, arr[0] replaced by 4
k = 2, arr = {4, 4, 6, 4, 15}, arr[3] replaced by 4
k = 3, arr = {4, 4, 6, 4, 4}, arr[4] replaced by 4
Max – Min = 6 – 4 = 2
Input: arr[] = {1, 4, 6, 11, 15}, k = 2
Output: 5
Explanation:
k = 1, arr = {1, 4, 6, 6, 15}, arr[3] replaced by 6
k = 2, arr = {1, 4, 6, 6, 6}, arr[4] replaced by 6
Max – Min = 6 – 1 = 5
Approach: The idea is to use the concept of Two Pointers. Below are the steps:
- Sort the given array.
- Maintain two pointers, one pointing to the last element of the array and the other to the Kth element of the array.
- Iterate over the array K + 1 times and each time find the difference of the elements pointed by the two pointers.
- Every time on finding the difference, keep track of the minimum possible difference in a variable and return that value at the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxMinDifference( int arr[], int n, int k)
{
if (k >= n - 1)
return 0;
sort(arr, arr + n);
int ans = arr[n - 1] - arr[0];
for ( int i = k, j = n - 1;
i >= 0; --i, --j) {
ans = min(arr[j] - arr[i], ans);
}
return ans;
}
int main()
{
int arr[] = { 1, 4, 6, 11, 15 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 3;
cout << maxMinDifference(arr, N, K);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG{
static int maxMinDifference( int arr[], int n, int k)
{
if (k >= n - 1 )
return 0 ;
Arrays.sort(arr);
int ans = arr[n - 1 ] - arr[ 0 ];
for ( int i = k, j = n - 1 ;
i >= 0 ; --i, --j)
{
ans = Math.min(arr[j] - arr[i], ans);
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 1 , 4 , 6 , 11 , 15 };
int N = arr.length;
int K = 3 ;
System.out.print(maxMinDifference(arr, N, K));
}
}
|
Python3
def maxMinDifference(arr, n, k):
if (k > = n - 1 ):
return 0
arr.sort()
ans = arr[n - 1 ] - arr[ 0 ]
i = k
j = n - 1
while i > = 0 :
ans = min (arr[j] - arr[i], ans)
i - = 1
j - = 1
return ans
if __name__ = = '__main__' :
arr = [ 1 , 4 , 6 , 11 , 15 ]
N = len (arr)
K = 3
print (maxMinDifference(arr, N, K))
|
C#
using System;
class GFG{
static int maxMinDifference( int []arr, int n, int k)
{
if (k >= n - 1)
return 0;
Array.Sort(arr);
int ans = arr[n - 1] - arr[0];
for ( int i = k, j = n - 1;
i >= 0; --i, --j)
{
ans = Math.Min(arr[j] - arr[i], ans);
}
return ans;
}
public static void Main( string [] args)
{
int [] arr = new int [] { 1, 4, 6, 11, 15 };
int N = arr.Length;
int K = 3;
Console.Write(maxMinDifference(arr, N, K));
}
}
|
Javascript
<script>
function maxMinDifference(arr, n, k)
{
if (k >= n - 1)
return 0;
arr.sort((a, b) => a - b);
let ans = arr[n - 1] - arr[0];
for (let i = k, j = n - 1;
i >= 0; --i, --j)
{
ans = Math.min(arr[j] - arr[i], ans);
}
return ans;
}
let arr = [ 1, 4, 6, 11, 15 ];
let N = arr.length;
let K = 3;
document.write(maxMinDifference(arr, N, K));
</script>
|
Time Complexity: O(N*log2N)
Auxiliary Space: O(1)
Last Updated :
12 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...