Count how many contiguous subarrays of length three satisfy a given condition.
You are given an integer array nums. Consider every contiguous subarray of length 3.
Count how many of these length-3 subarrays satisfy the required condition from the problem statement. Return the count.
The exact condition is simple and local to each triplet, so the intended solution is to scan the array once and evaluate each window of size three.
Input Format
- An integer array
nums. - You may assume
numscontains at least 3 elements for valid windows to exist.
Output Format
- Return an integer: the number of length-3 contiguous subarrays that satisfy the condition.
Constraints
3 <= nums.length- Elements are integers.
- Time-efficient solutions should inspect each length-3 window once.
Example 1
Input
nums = [1, 2, 3, 4, 5]
Output
3
Explanation
The length-3 windows are [1,2,3], [2,3,4], and [3,4,5]. For this illustrative version, each window satisfies the local condition, so the count is 3.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.