Remove Least Elements to Convert Array to Increasing Sequence using JavaScript
Given an array of numbers, our task is to find the minimum number of elements that need to be removed from the array so that the remaining elements form an increasing sequence.
Examples:
Input: arr = [5, 100, 6, 7, 100, 9, 8];
Output: No of elements removed =3, Array after removing elements =[ 5, 6 ,7,8]
Input: arr = [1, 100, 60, 7, 17, 19, 87];
Output: No of elements removed = 2, Array after removing elements =[1,7,17,19,87}
Below are the approaches to removing least number of elements to convert the array into an increasing sequence:
Table of Content
Using Greedy Approach
In this approach, we iterate through the array and identify elements that are not part of the increasing sequence. After that, we mark those elements for removal and return the resulting array after filtering out the marked elements.
Example: Implementation of program to remove least number of elements to convert array into increasing sequence using Greedy Approach
const arr = [5, 100, 6, 7, 100, 9, 8];
const findIncreasingArray = (arr = []) => {
// Create a copy of the original array
const copy = arr.slice();
// Variable to count elements marked for removal
let elementsRemoved = 0;
// Iterate through the array to mark elements for removal
for (let i = 0; i < copy.length - 1; i++) {
if (copy[i] > copy[i + 1]) {
copy[i] = undefined;
elementsRemoved++;
}
}
// Filter out marked elements and
// return the resulting array
const increasingSequence = copy.filter(Boolean);
// Print the number of elements removed
// and the resulting array
console.log("No of elements removed =", elementsRemoved);
console.log("Array after removing elements =", increasingSequence);
// Return the resulting array
return increasingSequence;
};
findIncreasingArray(arr);
Output
No of elements removed = 3 Array after removing elements = [ 5, 6, 7, 8 ]
Time Complexity: O(n)
Auxiliary Space: O(n)
Using Dynamic Programming
In this approach, we use dynamic programming to find the minimum number of elements that need to be removed to make the remaining elements form an increasing sequence.
Example: Implementation of program to remove least number of elements to convert array into increasing sequence using Dynamic Programming
const arr = [5, 100, 6, 7, 100, 9, 8];
const minElementsToRemove = (arr) => {
const n = arr.length;
// Initialize dp array with 1
const dp = new Array(n).fill(1);
for (let i = 1; i < n; i++) {
for (let j = 0; j < i; j++) {
if (arr[i] > arr[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
}
// Find the maximum value in dp array
const maxLength = Math.max(...dp);
// Minimum elements to remove will be total
// elements - maximum increasing subsequence length
const minToRemove = n - maxLength;
// Construct the increasing sequence array
const increasingSequence = [];
let currentLength = maxLength;
for (let i = n - 1; i >= 0 && currentLength > 0; i--) {
if (dp[i] === currentLength) {
increasingSequence.unshift(arr[i]);
currentLength--;
}
}
return {
minToRemove: minToRemove,
increasingSequence: increasingSequence
};
};
const result = minElementsToRemove(arr);
console.log("No of elements removed =", result.minToRemove);
console.log(
"Array after removing elements =", result.increasingSequence);
Output
No of elements removed = 3 Array after removing elements = [ 5, 6, 7, 8 ]
Time Complexity: O(N* 2),where n is the number of elements in the input array.
Auxiliary Space: O(n)