Remove Least Elements to Convert Array to Increasing Sequence using JavaScript
Last Updated :
08 May, 2024
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:
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
JavaScript
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);
OutputNo 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
JavaScript
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);
OutputNo 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)
Similar Reads
JavaScript Program to Remove Duplicate Elements From a Sorted Array Given a sorted array arr[] of size N, the task is to remove the duplicate elements from the array. Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} Explanation: All the elements are 2, So only keep one instance of 2. Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4,
3 min read
JavaScript Program to find the Longest Consecutive Sequence of Numbers in an Array In this article, we are going to find the longest consecutive sequence of numbers in an array using JavaScript language. We are given an array that consists of integers we need to find the longest subsequence such that elements in the subsequence are consecutive integers, these numbers are in any or
4 min read
K-th Smallest Element after Removing some Integers from Natural Numbers using JavaScript Given an array of size "n" and a positive integer k our task is to find the K-th smallest element that remains after removing certain integers from the set of natural numbers using JavaScript. Example:Input: array = [3, 5]k = 2Output: 2Explanation: The natural numbers are [1, 2, 3, 4, 5......] and a
5 min read
JavaScript Program to Remove Empty Strings from Array Without Loop Given an array with values and empty strings, our task is to remove empty strings from an array while keeping a record of the removed elements without using explicit loops. Example: Input: Array = ["hello", "", "world", "", "!", " "]; Output: Cleaned Array: [ 'hello', 'world', '!', ' ' ] Removed Ele
3 min read
JavaScript Program to Sort an Array which Contain 1 to n Values In this article, we will Sort an array that contains 1 to n values in JavaScript. There are different approaches through which we can sort an array. So, we will mainly cover three different approaches.We will explore all the above methods along with their basic implementation with the help of exampl
4 min read
Implement Custom Array Flat method in JavaScript The flat() method in JavaScript is used to flatten nested arrays. By using that function we can directly convert any type of array into 1D array. These are the following approaches to implementing a custom array flat method in JavaScript: Table of Content Using Recursion approachUsing Iterative appr
2 min read