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
Maximum decreasing adjacent elements in JavaScript
In JavaScript, finding the maximum number of decreasing adjacent elements means identifying the longest consecutive sequence where each element is smaller than the previous one. This is essentially finding the longest decreasing subarray.
Understanding the Problem
We need to find the maximum count of consecutive decreasing pairs in an array. For example, in array [5, 4, 3, 2, 1], we have 4 decreasing pairs: (5,4), (4,3), (3,2), and (2,1).
Algorithm
The solution uses a single-pass algorithm that tracks consecutive decreasing elements:
- Initialize
maxCountandcountto 0 - Iterate through the array starting from index 1
- If current element is less than previous, increment
count - Otherwise, update
maxCountif needed and resetcount - Check final
countafter loop ends
Implementation
function maxDecreasingAdjacent(arr) {
let maxCount = 0;
let count = 0;
// Iterate through array starting from second element
for (let i = 1; i < arr.length; i++) {
if (arr[i] < arr[i-1]) {
// Found decreasing pair, increment count
count++;
} else {
// Sequence broken, update maxCount if needed
if (count > maxCount) {
maxCount = count;
}
count = 0; // Reset current count
}
}
// Check final count in case array ends with decreasing sequence
if (count > maxCount) {
maxCount = count;
}
return maxCount;
}
// Test with different arrays
const array1 = [5, 4, 3, 2, 1]; // All decreasing
const array2 = [1, 2, 3, 4, 5]; // All increasing
const array3 = [5, 4, 3, 7, 6, 2, 1]; // Mixed pattern
console.log(`Array [${array1}] has ${maxDecreasingAdjacent(array1)} decreasing pairs`);
console.log(`Array [${array2}] has ${maxDecreasingAdjacent(array2)} decreasing pairs`);
console.log(`Array [${array3}] has ${maxDecreasingAdjacent(array3)} decreasing pairs`);
Array [5,4,3,2,1] has 4 decreasing pairs Array [1,2,3,4,5] has 0 decreasing pairs Array [5,4,3,7,6,2,1] has 3 decreasing pairs
How It Works
Let's trace through the mixed pattern example [5, 4, 3, 7, 6, 2, 1]:
- 5 ? 4: Decreasing, count = 1
- 4 ? 3: Decreasing, count = 2
- 3 ? 7: Increasing! Update maxCount = 2, reset count = 0
- 7 ? 6: Decreasing, count = 1
- 6 ? 2: Decreasing, count = 2
- 2 ? 1: Decreasing, count = 3
- End of array: Final maxCount = max(2, 3) = 3
Time and Space Complexity
| Complexity Type | Value | Explanation |
|---|---|---|
| Time Complexity | O(n) | Single pass through the array |
| Space Complexity | O(1) | Only uses constant extra space |
Edge Cases
// Edge cases
console.log("Empty array:", maxDecreasingAdjacent([])); // 0
console.log("Single element:", maxDecreasingAdjacent([5])); // 0
console.log("Two elements decreasing:", maxDecreasingAdjacent([5, 3])); // 1
console.log("All equal:", maxDecreasingAdjacent([3, 3, 3, 3])); // 0
Empty array: 0 Single element: 0 Two elements decreasing: 1 All equal: 0
Conclusion
This algorithm efficiently finds the maximum number of decreasing adjacent elements in O(n) time using a simple single-pass approach. The key insight is tracking the current decreasing sequence length while maintaining the maximum found so far.
