Minimize count of array elements to be removed to maximize difference between any pair up to K
Last Updated :
10 May, 2021
Given an array arr[] and an integer K, the task is to count the number of elements to be removed from the array such that the difference of the maximum and the minimum number left does not exceed K.
Examples:
Input: K = 1, arr[] = {1, 2, 3, 4, 5}
Output: 3
Explanation:
Removal of {5, 4, 3} modifies array to {1, 2} where the maximum difference is 1(= K).
Input: K = 3, arr[] = {1, 2, 3, 4, 5}
Output: 1
Explanation:
Removal of {5} modifies array to {1, 2, 3, 4} where the maximum difference is 3(= K).
Approach:
The task is to find the difference between the maximum and minimum array element which should not exceed K.
- Sort the array in ascending order and initialize a variable to a minimum value.
- Iterate over the array to generate all possible pairs and check if the difference between any pair is less than or equal to K.
- Update the minimum number of removals for each pair.
- Finally, print the minimum removals obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int min_remove( int arr[], int n, int k)
{
sort(arr, arr + n);
int ans = INT_MAX;
for ( int i = 0; i < n; i++) {
for ( int j = i; j < n; j++) {
if (arr[j] - arr[i] <= k) {
ans = min(ans, n - j + i - 1);
}
}
}
return ans;
}
int main()
{
int k = 3;
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof arr / sizeof arr[0];
cout << min_remove(arr, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int min_remove( int arr[], int n, int k)
{
Arrays.sort(arr);
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i; j < n; j++)
{
if (arr[j] - arr[i] <= k)
{
ans = Math.min(ans, n - j + i - 1 );
}
}
}
return ans;
}
public static void main(String[] args)
{
int k = 3 ;
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
System.out.print(min_remove(arr, n, k));
}
}
|
Python3
import sys
def min_remove(arr, n, k):
arr.sort()
ans = sys.maxsize
for i in range (n):
for j in range (i, n):
if (arr[j] - arr[i] < = k):
ans = min (ans, n - j + i - 1 )
return ans
if __name__ = = "__main__" :
k = 3
arr = [ 1 , 2 , 3 , 4 , 5 ]
n = len (arr)
print (min_remove(arr, n, k))
|
C#
using System;
class GFG{
static int min_remove( int []arr, int n, int k)
{
Array.Sort(arr);
/// Initialize the variable
int ans = int .MaxValue;
for ( int i = 0; i < n; i++)
{
for ( int j = i; j < n; j++)
{
if (arr[j] - arr[i] <= k)
{
ans = Math.Min(ans, n - j + i - 1);
}
}
}
return ans;
}
public static void Main(String[] args)
{
int k = 3;
int []arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
Console.Write(min_remove(arr, n, k));
}
}
|
Javascript
<script>
function min_remove(arr, n, k)
{
arr.sort();
let ans = Number.MAX_VALUE;
for (let i = 0; i < n; i++)
{
for (let j = i; j < n; j++)
{
if (arr[j] - arr[i] <= k)
{
ans = Math.min(ans, n - j + i - 1);
}
}
}
return ans;
}
let k = 3;
let arr = [ 1, 2, 3, 4, 5 ];
let n = arr.length;
document.write(min_remove(arr, n, k));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...