Skip to main content

Maximum Length of Semi-Decreasing Subarrays - Video Solutions

MediumArrayStackSortingMonotonic Stack

leetcode 2863 [Google Interview Q]. Maximum Length of Semi-Decreasing Subarrays - monotonic stack

Code-Yao
11:411,448 views
5 video solutions available

Maximum Length of Semi-Decreasing Subarrays - Video Solution

Watch 5 video solutions for Maximum Length of Semi-Decreasing Subarrays, a medium level problem involving Array, Stack, Sorting. This walkthrough by Code-Yao has 1,448 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

You are given an integer array nums.

Return the length of the longest semi-decreasing subarray of nums, and 0 if there are no such subarrays.

  • A subarray is a contiguous non-empty sequence of elements within an array.
  • A non-empty array is semi-decreasing if its first element is strictly greater than its last element.

 

Example 1:

Input: nums = [7,6,5,4,3,2,1,6,10,11]
Output: 8
Explanation: Take the subarray [7,6,5,4,3,2,1,6].
The first element is 7 and the last one is 6 so the condition is met.
Hence, the answer would be the length of the subarray or 8.
It can be shown that there aren't any subarrays with the given condition with a length greater than 8.

Example 2:

Input: nums = [57,55,50,60,61,58,63,59,64,60,63]
Output: 6
Explanation: Take the subarray [61,58,63,59,64,60].
The first element is 61 and the last one is 60 so the condition is met.
Hence, the answer would be the length of the subarray or 6.
It can be shown that there aren't any subarrays with the given condition with a length greater than 6.

Example 3:

Input: nums = [1,2,3,4]
Output: 0
Explanation: Since there are no semi-decreasing subarrays in the given array, the answer is 0.

 

Constraints:

  • 1 <= nums.length <= 105
  • -109 <= nums[i] <= 109
Read full problem with examples

Approach Overview

Problem Overview: You are given an integer array nums. A subarray is semi-decreasing if the first element of the subarray is strictly greater than the last element. The task is to return the maximum length of such a subarray.

The key observation: for any pair of indices i < j, the subarray nums[i..j] is valid when nums[i] > nums[j]. The problem becomes finding the largest distance between two indices that satisfy this condition.

Approach 1: Brute Force Scan (O(n²) time, O(1) space)

Check every possible subarray. For each starting index i, iterate j from i+1 to the end and verify whether nums[i] > nums[j]. If the condition holds, update the maximum length j - i + 1. This approach directly follows the definition of a semi-decreasing subarray and is useful for validating edge cases or small inputs. However, the nested loop leads to O(n²) time complexity, which becomes too slow for large arrays.

Approach 2: Hash Table + Sorting (O(n log n) time, O(n) space)

Sort the array values while keeping their original indices. While iterating through the sorted values, maintain the smallest index seen so far for larger elements. For a current value nums[j], you want the earliest index i such that nums[i] > nums[j]. Sorting groups numbers by value, which makes it easy to process larger elements first and store their indices in a hash table or auxiliary structure. Each step computes a candidate length j - i + 1. Sorting dominates the complexity at O(n log n), and additional storage requires O(n) space. This approach is intuitive if you are comfortable with sorting based transformations.

Approach 3: Monotonic Stack (O(n) time, O(n) space)

A more efficient strategy uses a decreasing stack of indices. Traverse the array from left to right and push an index onto the stack whenever it forms a strictly decreasing sequence of values. The stack now stores candidate starting points where the value is relatively large. Next, traverse the array from right to left. For each index j, repeatedly check the top of the stack while nums[stack.top()] > nums[j]. Each valid pair produces a subarray length j - stack.top() + 1, and the index is popped because any future j would only produce a shorter length. This technique relies on the same idea used in many monotonic stack problems and processes each index at most once, giving O(n) time.

Recommended for interviews: Start by describing the brute force idea to demonstrate understanding of the condition nums[i] > nums[j]. Then move to the monotonic stack optimization. Interviewers typically expect the O(n) solution because it shows familiarity with array scanning patterns and stack-based optimizations.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force ScanO(n²)O(1)Useful for understanding the problem or validating small inputs
Hash Table + SortingO(n log n)O(n)Clear logic using sorted values and index tracking
Monotonic StackO(n)O(n)Optimal approach for large arrays and typical interview expectations