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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read