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).

Finding Decreasing Adjacent Elements 5 arr[0] 4 arr[1] 3 arr[2] 2 arr[3] 1 arr[4] 5 > 4 ? 4 > 3 ? 3 > 2 ? 2 > 1 ? Result: 4 decreasing pairs

Algorithm

The solution uses a single-pass algorithm that tracks consecutive decreasing elements:

  1. Initialize maxCount and count to 0
  2. Iterate through the array starting from index 1
  3. If current element is less than previous, increment count
  4. Otherwise, update maxCount if needed and reset count
  5. Check final count after 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.

Updated on: 2026-03-15T23:19:00+05:30

257 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements