# Maximum Length Of Semi Decreasing Subarrays: Walkthrough
Maximum Length Of Semi Decreasing Subarrays shows up often in coding rounds. Here is how I talk through it when the interviewer is watching.
## Key idea
A semi-decreasing subarray has the first element strictly greater than the last. Maximize its length, or 0 if none exists. The longest such subarray is either from the leftmost element that is greater than some later element, which in practice is n - i where i is the leftmost index that is not part of the non-decreasing suffix? More simply: the answer is n - (index of the leftmost value that is strictly greater than the minimum of the suffix after it). Scan from the right tracking the suffix min; the leftmost i with nums[i] > suffixMin gives the longest window i..n-1, but a later start with a much smaller suffix can be longer only if... actually the longest is max(j-i+1) over nums[i] > nums[j], which equals n - min{i : nums[i] > some later value} when we take j = n-1 whenever nums[i] > nums[n-1], otherwise we need the rightmost greater-pair. Equivalent clean approach: the answer is 0 if the array is non-decreasing; else it is the max of (n - first_index_not_in_the_nondecreasing_prefix_that_still_has_a_smaller_later) — implement by tracking the running max from the left and the first position a drop happens, then the longest window ends at the last index that is < some earlier value. A reliable O(n) way: find the longest prefix that is <= all later? Practically: iterate from the right with a monotonic stack of candidates for nums[j]; for each i from the left, pop stack values >= nums[i] and the farthest remaining j is the best end for this start.
## Steps to say out loud
1. Restate the input and the exact ask, and nail down constraints and overflow.
2. Name the pattern (binary search on the answer, monotonic stack or deque, interval / interval-DP, string hashing, graph traversal, or shortest paths) and why a nested scan or naive search fails.
3. Code it, then trace a short example plus one empty or already-solved edge case.
## Complexity
O(n).
## Common mistake
Returning n whenever any descent exists without checking that the endpoints themselves satisfy first > last (the interior can go up and down).
## Practice tip
Time yourself: two minutes to explain, ten to code, three to test edges. Disclosure: I work on Linkjob AI. I also keep a practice cheat sheet at https://linkjob.ai.