Minimize count of array elements to be removed to maximize difference between any pair up to K | C # Program to implement the above approach ; Function to count the number of elements to be removed from the array based on the given condition ; Sort the array ; / Initialize the variable ; Iterate for all possible pairs ; Check the difference between the numbers ; Update the minimum removals ; Return the answer ; Driver Code
using System ; class GFG { static int min_remove ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; 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 ) ) ; } }