JavaScript Program to Find the length of the Longest Continuous Decreasing Subarray
Last Updated :
24 Jun, 2024
Given an array of numbers, our task is to find the length of the longest continuous subarray where the numbers are strictly decreasing.
Example:
Input:
const arr = [7, 4, 7, 6, 5, 4, 6, 8, 9]
Output: 4
Explanation: The longest continuous decreasing subarray in this array is [7, 6, 5, 4], and its length is 4.
Below are the approaches to Find the length of the longest continuous decreasing subarray:
Iterative Approach
The code initializes maxLength and currentLength. It iterates through the array, incrementing currentLength if the current element is less than the previous one; otherwise, it updates maxLength with the max of maxLength and currentLength, and resets currentLength to 1 for a new subarray. Finally, it returns maxLength, representing the length of the longest decreasing subarray.
Example: The example below shows how to Find the length of the longest continuous decreasing subarray using Iterative Approach.
JavaScript
function longestDec(arr) {
let max = 0;
let cur = 1;
for (let i = 1; i < arr.length; i++) {
if (arr[i] < arr[i - 1]) {
cur++;
} else {
max = Math.max(max, cur);
cur = 1;
}
}
max = Math.max(max, cur);
return max;
}
const arr = [7, 4, 7, 6, 5, 4, 6, 8, 9];
console.log(longestDec(arr));
Time Complexity: O(n), where n is the size of the input array.
Auxiliary Space: O(1), which means it requires a constant amount of extra space.
Dynamic Programming Approach
Initialize an array dp of the same length as the input array. Each element dp[i] will be initially set to 1. Iterate through the array starting from the second element. For each element at index i, compare it with the previous element. If the current element is less than the previous element, extend the longest decreasing subarray ending at index i by setting dp[i] = dp[i - 1] + 1. If current ≥ previous, it marks the beginning of a new decreasing subarray, setting dp[i] to 1. Then, after the loop, the maximum value in dp denotes the longest decreasing subarray length, returned as the result.
Example: The example below shows how to Find the length of the longest continuous decreasing subarray using Dynamic Programming Approach.
JavaScript
function longestDecSub(arr) {
const n = arr.length;
const dp = new Array(n).fill(1);
for (let i = 1; i < n; i++) {
if (arr[i] < arr[i - 1]) {
dp[i] = dp[i - 1] + 1;
}
}
const maxLen = Math.max(...dp);
return maxLen;
}
const arr = [9, 6, 9, 8, 7, 6, 5, 10, 12];
console.log(longestDecSub(arr));
Time Complexity: O(n), where n is the size of the input array.
Auxiliary Space: O(n), where n is the size of the input array.
Using Single Pass Approach with a Counter Variable
This single-pass approach iterates through the array, updating a counter for the current decreasing sequence length. It tracks the maximum length encountered, resetting when a non-decreasing element is found, ensuring efficient identification of the longest continuous decreasing subarray.
Example:
JavaScript
function longestDecreasingSubarraySinglePass(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;
}
}
maxLength = Math.max(maxLength, currentLength);
return maxLength;
}
let arr = [9, 8, 7, 3, 2, 1, 4, 3, 2, 1, 0];
console.log(`The length of the longest continuous decreasing subarray is:
${longestDecreasingSubarraySinglePass(arr)}`);
OutputThe length of the longest continuous decreasing subarray is:
6
Similar Reads
Javascript Program for Largest Sum Contiguous Subarray Write an efficient program to find the sum of contiguous subarray within a one-dimensional array of numbers that has the largest sum. Kadane's Algorithm:Initialize: max_so_far = INT_MIN max_ending_here = 0Loop for each element of the array (a) max_ending_here = max_ending_here + a[i] (b) if(max_so_f
5 min read
Javascript Program to Find the Longest Bitonic Subsequence | DP-15 Given an array arr[0 ... n-1] containing n positive integers, a subsequence of arr[] is called Bitonic if it is first increasing, then decreasing. Write a function that takes an array as argument and returns the length of the longest bitonic subsequence. A sequence, sorted in increasing order is con
3 min read
Javascript Program for Size of The Subarray With Maximum Sum An array is given, find length of the subarray having maximum sum.Examples : Input : a[] = {1, -2, 1, 1, -2, 1}Output : Length of the subarray is 2Explanation: Subarray with consecutive elements and maximum sum will be {1, 1}. So length is 2Input : ar[] = { -2, -3, 4, -1, -2, 1, 5, -3 }Output : Leng
2 min read
Javascript Program for Shortest Un-ordered Subarray An array is given of n length, and the problem is that we have to find the length of the shortest unordered {neither increasing nor decreasing} subarray in the given array.Examples: Input : n = 5 7 9 10 8 11Output : 3Explanation : 9 10 8 unordered sub array.Input : n = 5 1 2 3 4 5Output : 0 Explanat
2 min read
Longest Increasing Subsequence in C In this article, we will learn how to find the Longest Increasing Subsequence (LIS) of a given sequence using C programming language. LIS is the longest subsequence of a sequence such that all elements of the subsequence are sorted in increasing order.Example:Input:Sequence: [10, 22, 9, 33, 21, 50,
9 min read
Longest Increasing Subsequence in C++ In this article, we will learn the concept of the Longest Increasing Subsequence (LIS) in a given sequence using C++ language. The LIS problem is about finding the longest subsequence of a sequence in which the elements are in sorted order, from lowest to highest, and not necessarily contiguous.Exam
9 min read
Longest Increasing consecutive subsequence Given N elements, write a program that prints the length of the longest increasing consecutive subsequence. Examples: Input : a[] = {3, 10, 3, 11, 4, 5, 6, 7, 8, 12} Output : 6 Explanation: 3, 4, 5, 6, 7, 8 is the longest increasing subsequence whose adjacent element differs by one. Input : a[] = {6
10 min read
Longest subarray in which all elements are greater than K Given an array of N integers and a number K, the task is to find the length of the longest subarray in which all the elements are greater than K. Examples: Input: a[] = {3, 4, 5, 6, 7, 2, 10, 11}, K = 5 Output: 2 There are two possible longest subarrays of length 2. They are {6, 7} and {10, 11}. Inp
6 min read
Java Program for Longest Increasing Subsequence The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order. For example, the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60,
5 min read
Find the length of Longest increasing Consecutive Subarray Given an array arr[] of N integers, the task is to find the length of the longest increasing subarray such that elements in the subarray are consecutive integers. Examples: Input: arr[] = {1, 9, 3, 4, 20, 2}Output: 2Explanation: The subarray {3, 4} is the longest subarray of consecutive elements Inp
4 min read