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++
// C++ Program to implement// the above approach#include <bits/stdc++.h>using namespace std;// Function to count the number of// elements to be removed from the// array based on the given conditionint min_remove(int arr[], int n, int k){ // Sort the array sort(arr, arr + n); /// Initialize the variable int ans = INT_MAX; // Iterate for all possible pairs for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // Check the difference // between the numbers if (arr[j] - arr[i] <= k) { // Update the minimum removals ans = min(ans, n - j + i - 1); } } } // Return the answer return ans;}// Driver Codeint 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
// Java Program to implement// the above approachimport java.util.*;class GFG{// Function to count the number of// elements to be removed from the// array based on the given conditionstatic int min_remove(int arr[], int n, int k){ // Sort the array Arrays.sort(arr); /// Initialize the variable int ans = Integer.MAX_VALUE; // Iterate for all possible pairs for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // Check the difference // between the numbers if (arr[j] - arr[i] <= k) { // Update the minimum removals ans = Math.min(ans, n - j + i - 1); } } } // Return the answer return ans;}// Driver Codepublic 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));}}// This code is contributed by sapnasingh4991 |
C#
// C# Program to implement// the above approachusing System;class GFG{// Function to count the number of// elements to be removed from the// array based on the given conditionstatic int min_remove(int []arr, int n, int k){ // Sort the array Array.Sort(arr); /// Initialize the variable int ans = int.MaxValue; // Iterate for all possible pairs for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // Check the difference // between the numbers if (arr[j] - arr[i] <= k) { // Update the minimum removals ans = Math.Min(ans, n - j + i - 1); } } } // Return the answer return ans;}// Driver Codepublic 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));}}// This code is contributed by sapnasingh4991 |
1
Time Complexity: O(N2)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimize swaps required to maximize the count of elements replacing a greater element in an Array
- Rearrange an array to minimize sum of product of consecutive pair elements
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Minimize count of divisions by D to obtain at least K equal array elements
- Minimize the maximum difference between adjacent elements in an array
- Maximize count of distinct elements possible in an Array from the given operation
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Count of elements to be multiplied with integers to make each pair of Array a perfect square
- Minimum elements to be removed to make count of 0s and 1s equal
- Check if all array elements can be removed by the given operations
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Shortest subarray to be removed to make all Array elements unique
- Minimum elements to be removed from the ends to make the array sorted
- Minimum number of elements that should be removed to make the array good
- Minimize difference after changing all odd elements to even
- Find the minimum number of elements that should be removed to make an array good
- Count of elements such that its sum/difference with X also exists in the Array
- Minimize the difference between minimum and maximum elements
- Minimize the maximum difference of adjacent elements after at most K insertions
- Maximize sum of atmost K elements in array by taking only corner elements | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : sapnasingh4991

