
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Minimum removals from array to make max – min <= K in C++
Problem statement
Given N integers and K, find the minimum number of elements that should be removed such that Amax - Amin <= K. After removal of elements, Amax and Amin is considered among the remaining elements
Examples
If arr[] = {1, 3, 4, 9, 10, 11, 12, 17, 20} and k = 4 then output would be 5:
- Remove 1, 3 and 4 from beginning of an array
- Remove 17 and 20 from the end of array
- Final array becomes {9, 10, 11, 12} where 12 – 9 <= 4
Algorithm
1. Sort the given elements 2. Using greedy approach, the best way is to remove the minimum element or the maximum element and then check if Amax - Amin <= K. There are various combinations of removals that have to be considered. 3. There will be two possible ways of removal, either we remove the minimum or we remove the maximum. Let(i…j) be the index of elements left after removal of elements. Initially, we start with i=0 and j=n-1 and the number of elements removed is 0 at the beginnings. 4. We only remove an element if a[j]-a[i]>k, the two possible ways of removal are (i+1…j) or (i…j-1). The minimum of the two is considered
Example
#include <bits/stdc++.h> #define MAX 100 using namespace std; int dp[MAX][MAX]; int removeCombinations(int *arr, int i, int j, int k) { if (i >= j) { return 0; } else if ((arr[j] - arr[i]) <= k) { return 0; } else if (dp[i][j] != -1) { return dp[i][j]; } else if ((arr[j] - arr[i]) > k) { dp[i][j] = 1 + min(removeCombinations(arr, i + 1, j, k), removeCombinations(arr, i, j - 1,k)); } return dp[i][j]; } int removeNumbers(int *arr, int n, int k){ sort(arr, arr + n); memset(dp, -1, sizeof(dp)); return n == 1 ? 0 : removeCombinations(arr, 0, n - 1,k); } int main() { int arr[] = {1, 3, 4, 9, 10, 11, 12, 17, 20}; int n = sizeof(arr) / sizeof(arr[0]); int k = 4; cout << "Minimum numbers to be removed = " << removeNumbers(arr, n, k) << endl; return 0; }
When you compile and execute above program. It generates following output
Output
Minimum numbers to be removed = 5
- Related Articles
- Minimum removals from array to make GCD Greater in Python
- Minimum removals from array to make GCD Greater in C++
- Minimum removals to make array sum even in C++
- Minimum removals to make array sum odd in C++
- Minimum value of “max + min” in a subarray in C++
- Min-Max Range Queries in Array in C++
- Average of array excluding min max JavaScript
- How to find Min/Max numbers in a java array?
- Minimum removals in a number to be divisible by 10 power raised to K in C++
- Min and max values of an array in MongoDB?
- Minimum operations to make GCD of array a multiple of k in C++
- C++ program to remove minimum elements from either side such that 2*min becomes more than max
- Min-Max Heaps
- Program to find sum of differences between max and min elements from randomly selected k balls from n balls in Python
- Sort array based on min and max date in JavaScript?

Advertisements