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.
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] <= 109Problem 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.
The problem is essentially finding the maximum length of the inverse pairs. We can use a hash table d to record the index i corresponding to each number x in the array.
Next, we traverse the keys of the hash table in descending order of the numbers. We maintain a number k to keep track of the smallest index that has appeared so far. For the current number x, we can get a maximum inverse pair length of d[x][|d[x]|-1]-k + 1, where |d[x]| represents the length of the array d[x], i.e., the number of times the number x appears in the original array. We update the answer accordingly. Then, we update k to d[x][0], which is the index where the number x first appears in the original array. We continue to traverse the keys of the hash table until all keys are traversed.
The time complexity is O(n times log n), and the space complexity is O(n). Here, n is the length of the array nums.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Scan | O(n²) | O(1) | Useful for understanding the problem or validating small inputs |
| Hash Table + Sorting | O(n log n) | O(n) | Clear logic using sorted values and index tracking |
| Monotonic Stack | O(n) | O(n) | Optimal approach for large arrays and typical interview expectations |
leetcode 2863 [Google Interview Q]. Maximum Length of Semi-Decreasing Subarrays - monotonic stack • Code-Yao • 1,447 views views
Watch 3 more video solutions →Practice Maximum Length of Semi-Decreasing Subarrays with our built-in code editor and test cases.
Practice on FleetCode