Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Longest decreasing subsequence subarray in JavaScript
We are required to write a JavaScript function that takes in an array of integers and returns the length of the longest consecutive decreasing subsequence (subarray) from the array.
Problem Statement
Given an array of integers, find the length of the longest contiguous subsequence where each element is smaller than the previous one.
For example, if the input array is:
const arr = [5, 2, 5, 4, 3, 2, 4, 6, 7];
The longest decreasing subsequence is [5, 4, 3, 2] with length 4.
Solution Approach
We'll track the current decreasing sequence and compare it with the longest found so far. When we encounter an element that breaks the decreasing pattern, we reset and start a new sequence.
Implementation
const arr = [5, 2, 5, 4, 3, 2, 4, 6, 7];
const decreasingSequence = (arr = []) => {
let longest = [];
let curr = [];
const setDefault = (newItem) => {
if (curr.length > longest.length) {
longest = curr;
}
curr = newItem ? [newItem] : [];
};
for (const item of arr) {
if (curr.length && item >= curr[curr.length - 1]) {
setDefault(item);
} else {
curr.push(item);
}
}
setDefault();
return longest.length;
};
console.log(decreasingSequence(arr));
4
How It Works
The algorithm maintains two arrays:
- curr: Current decreasing sequence being built
- longest: The longest decreasing sequence found so far
For each element, if it's greater than or equal to the last element in the current sequence, we save the current sequence (if it's the longest) and start fresh. Otherwise, we add the element to continue the decreasing pattern.
Step-by-Step Trace
const traceDecreasingSequence = (arr = []) => {
let longest = [];
let curr = [];
console.log("Tracing array:", arr);
for (let i = 0; i < arr.length; i++) {
const item = arr[i];
if (curr.length && item >= curr[curr.length - 1]) {
if (curr.length > longest.length) {
longest = [...curr];
}
curr = [item];
} else {
curr.push(item);
}
console.log(`Step ${i + 1}: item=${item}, curr=[${curr}], longest=[${longest}]`);
}
if (curr.length > longest.length) {
longest = [...curr];
}
console.log("Final longest length:", longest.length);
return longest.length;
};
traceDecreasingSequence([5, 2, 5, 4, 3, 2, 4, 6, 7]);
Tracing array: [ 5, 2, 5, 4, 3, 2, 4, 6, 7 ] Step 1: item=5, curr=[5], longest=[] Step 2: item=2, curr=[5,2], longest=[] Step 3: item=5, curr=[5], longest=[5,2] Step 4: item=4, curr=[5,4], longest=[5,2] Step 5: item=3, curr=[5,4,3], longest=[5,2] Step 6: item=2, curr=[5,4,3,2], longest=[5,2] Step 7: item=4, curr=[4], longest=[5,4,3,2] Step 8: item=6, curr=[6], longest=[5,4,3,2] Step 9: item=7, curr=[7], longest=[5,4,3,2] Final longest length: 4
Alternative Simplified Approach
const longestDecreasingLength = (arr) => {
if (arr.length === 0) return 0;
let maxLength = 1;
let currentLength = 1;
for (let i = 1; i < arr.length; i++) {
if (arr[i] < arr[i - 1]) {
currentLength++;
} else {
maxLength = Math.max(maxLength, currentLength);
currentLength = 1;
}
}
return Math.max(maxLength, currentLength);
};
console.log(longestDecreasingLength([5, 2, 5, 4, 3, 2, 4, 6, 7]));
console.log(longestDecreasingLength([9, 8, 7, 6, 5]));
console.log(longestDecreasingLength([1, 2, 3, 4, 5]));
4 5 1
Conclusion
Both approaches find the longest consecutive decreasing subsequence efficiently in O(n) time. The simplified version is more readable and handles edge cases better, making it the preferred solution for this problem.
