My first instinct was to brute force it with nested loops and I started explaining that before catching myself.
Clarify the problem and edge cases, then propose an O(n) single-pass solution that tracks the length of the current strictly decreasing run where each element is exactly one less than the previous. For each run of length L, the number of valid subarrays of length >=2 is L*(L-1)/2, which can be accumulated on the fly.
Pro tip: Mention that you can compute the count in one pass without extra space, and explicitly handle edge cases like empty arrays, single-element arrays, and non-consecutive decreases (e.g., [5,3,1]). This shows attention to detail and efficiency.
Restate the problem in your own words and ask clarifying questions about input constraints, expected output type, and edge cases (e.g., empty array, single element, duplicates).
Recognize that valid subarrays are contiguous segments where each adjacent pair satisfies arr[i+1] == arr[i] - 1. The length of such a segment determines the number of valid subarrays.
For a decreasing consecutive run of length L, any subarray of length >=2 within it is valid. The number of such subarrays is L*(L-1)/2. Explain why this formula works.
Use a single pass: initialize run_length = 1 and count = 0. For each i from 1 to n-1, if arr[i] == arr[i-1] - 1, increment run_length; else, add run_length*(run_length-1)/2 to count and reset run_length to 1. After the loop, add the final run's contribution.
State that time complexity is O(n) and space complexity is O(1). Walk through edge cases: empty array returns 0, single element returns 0, no valid runs returns 0, and a full decreasing run returns n*(n-1)/2.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.