Leetcode 3392. Count Subarrays of Length Three With a Condition
Count how many contiguous subarrays of length three satisfy that the sum of the first and third elements equals exactly half of the middle element (i.e., each length‑3 window must meet that arithmetic relation).
Asked at:
Cognizant
DESCRIPTION
Count how many contiguous subarrays of length three satisfy that the sum of the first and third elements equals exactly half of the middle element (i.e., each length‑3 window must meet that arithmetic relation).
Input:
nums = [2, 4, 1, 3, 5]
Output:
0
Explanation: No window of length 3 satisfies the condition. Window [2,4,1]: 2+1=3 but 4/2=2. Window [4,1,3]: 4+3=7 but 1/2=0.5. Window [1,3,5]: 1+5=6 but 3/2=1.5.
Constraints:
- 1 <= nums.length <= 10^5
- -10^9 <= nums[i] <= 10^9
- All elements are integers
- Need to check the condition: first + third == middle / 2
Understanding the Problem
Let's understand what we're being asked to do. We have an array like [2, 4, 1, 3, 5] and need to count how many contiguous subarrays of length 3 satisfy a specific arithmetic condition.
For each window of 3 consecutive elements [a, b, c], we check: does a + c equal exactly half of b? In other words, is a + c = b / 2?
Let's trace through the example [2, 4, 1, 3, 5]:
- Window [2, 4, 1]: Check if 2 + 1 = 4 / 2 → 3 = 2? No.
- Window [4, 1, 3]: Check if 4 + 3 = 1 / 2 → 7 = 0.5? No.
- Window [1, 3, 5]: Check if 1 + 5 = 3 / 2 → 6 = 1.5? No.
So the count is 0 for this example.
Important constraint: We're looking at every possible contiguous subarray of length 3, sliding through the array one position at a time.
Edge cases to consider: What if the array has fewer than 3 elements? (return 0). What if the middle element is odd (so b / 2 is not an integer)? What if the sum a + c is negative?
Building Intuition
We need to examine every consecutive triplet in the array. For an array of length n, there are exactly n - 2 such triplets (starting at indices 0, 1, 2, ..., n-3).
For each triplet [nums[i], nums[i+1], nums[i+2]], we simply check the arithmetic condition: nums[i] + nums[i+2] == nums[i+1] / 2.
This is a sliding window of fixed size 3. We don't need to track complex state or remember previous windows - each window is independent.
The key insight is that we can check each window in constant time O(1), and we have O(n) windows to check, giving us linear time overall.
Imagine sliding a frame of width 3 across the array, one position at a time:
[2, 4, 1, 3, 5]
[---] Check: 2 + 1 vs 4/2
[---] Check: 4 + 3 vs 1/2
[---] Check: 1 + 5 vs 3/2
At each position, we perform a simple arithmetic check. Count how many positions satisfy the condition. This is the essence of the fixed-size sliding window pattern.
Common Mistakes
Optimal Solution
Use a fixed-size sliding window of length 3 to iterate through the array. For each window starting at index i, extract the three elements [nums[i], nums[i+1], nums[i+2]] and check if nums[i] + nums[i+2] equals exactly nums[i+1] / 2. Increment a counter each time the condition is satisfied. This approach processes each element at most once as the window slides, achieving linear time complexity.
start
Start counting valid subarrays
0 / 11
Test Your Knowledge
Login to take the complexity quiz and track your progress
Complexity Analysis
Time Complexity: O(n) We iterate through the array once with a sliding window, checking n-2 windows where n is the array length. Each window check is O(1), so total time is O(n).
Space Complexity: O(1) We only use a constant amount of extra space for the counter and loop variables, regardless of input size.
What We've Learned
- Fixed-size sliding window pattern: When a problem asks about contiguous subarrays of fixed length (here, length 3), use a sliding window that moves one element at a time - this transforms an O(n*k) nested loop approach into O(n) by reusing overlapping elements between consecutive windows.
- Direct arithmetic validation: For simple element relationship checks within small windows, directly access elements by index (arr[i], arr[i+1], arr[i+2]) rather than maintaining complex window state - this keeps code readable and avoids unnecessary data structure overhead.
- Boundary-aware iteration: When sliding a window of size k, iterate only until index n-k (or i+k-1 < n) to prevent index-out-of-bounds errors - the last valid window starts at position n-k, not n-1, which is a common off-by-one mistake.
- Floating-point precision handling: When checking if arr[i] + arr[i+2] equals arr[i+1]/2, be cautious with division - either multiply both sides (2*(arr[i] + arr[i+2]) == arr[i+1]) to avoid floating-point errors, or use appropriate epsilon comparison for decimal values.
- Counter accumulation pattern: For problems counting subarrays that satisfy conditions, use a simple counter variable incremented when conditions are met - this is more efficient than storing qualifying subarrays in a list when only the count is needed.
- Window size generalization: This technique extends to any fixed-window constraint problem (k-element sums, products, averages, or custom predicates) - recognize that fixed-size windows with element-wise operations are ideal candidates for O(n) linear scans.
Related Concepts and Problems to Practice
easy
This problem practices the exact same pattern - iterating through fixed-size windows (size K) and performing calculations on elements within each window. The main difference is computing a sum versus checking an arithmetic relation, but the sliding window mechanics are identical.
medium
This problem extends the fixed-length sliding window pattern by adding an additional constraint (distinctness) while maintaining the core technique of examining fixed-size contiguous subarrays, similar to checking the arithmetic relation in windows of size 3.
Test Your Understanding
Why is sliding-window the right data structure for this problem?
sliding-window provides the optimal access pattern
It's the only data structure that works
It's the easiest to implement
It uses the least memory
Select an answer to see the explanation
Question Timeline
See when this question was last asked and where, including any notes left by other candidates.
Late November, 2025
Cognizant
Senior
Comments
Hello Interview Premium
Your account is free and you can post anonymously if you choose.