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
Selected Reading
Longest subarray which only contains strictly increasing numbers JavaScript
We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument.
The function should then return the length of the longest continuous subarray from the array that only contains elements in a strictly increasing order.
A strictly increasing sequence is the one in which any succeeding element is greater than all its preceding elements.
Example
const arr = [5, 7, 8, 12, 4, 56, 6, 54, 89];
const findLongest = (arr) => {
if(arr.length == 0) {
return 0;
};
let max = 0;
let count = 0;
for(let i = 1; i < arr.length; i++) {
if(arr[i] > arr[i-1]) {
count++; }
else {
count = 0;
}
if(count > max) {
max = count;
}
}
return max + 1;
};
console.log(findLongest(arr));
Output
And the output in the console will be −
4
Advertisements
