Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an integer array nums, return the number of subarrays of length 3 such that the sum of the first and third numbers equals exactly half of the second number.
Example 1:
Input: nums = [1,2,1,4,1]
Output: 1
Explanation:
Only the subarray [1,4,1] contains exactly 3 elements where the sum of the first and third numbers equals half the middle number.
Example 2:
Input: nums = [1,1,1]
Output: 0
Explanation:
[1,1,1] is the only subarray of length 3. However, its first and third numbers do not add to half the middle number.
Constraints:
3 <= nums.length <= 100-100 <= nums[i] <= 100Problem summary: Given an integer array nums, return the number of subarrays of length 3 such that the sum of the first and third numbers equals exactly half of the second number.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,2,1,4,1]
[1,1,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3392: Count Subarrays of Length Three With a Condition
class Solution {
public int countSubarrays(int[] nums) {
int ans = 0;
for (int i = 1; i + 1 < nums.length; ++i) {
if ((nums[i - 1] + nums[i + 1]) * 2 == nums[i]) {
++ans;
}
}
return ans;
}
}
// Accepted solution for LeetCode #3392: Count Subarrays of Length Three With a Condition
func countSubarrays(nums []int) (ans int) {
for i := 1; i+1 < len(nums); i++ {
if (nums[i-1]+nums[i+1])*2 == nums[i] {
ans++
}
}
return
}
# Accepted solution for LeetCode #3392: Count Subarrays of Length Three With a Condition
class Solution:
def countSubarrays(self, nums: List[int]) -> int:
return sum(
(nums[i - 1] + nums[i + 1]) * 2 == nums[i] for i in range(1, len(nums) - 1)
)
// Accepted solution for LeetCode #3392: Count Subarrays of Length Three With a Condition
impl Solution {
pub fn count_subarrays(nums: Vec<i32>) -> i32 {
let mut ans = 0;
for i in 1..nums.len() - 1 {
if (nums[i - 1] + nums[i + 1]) * 2 == nums[i] {
ans += 1;
}
}
ans
}
}
// Accepted solution for LeetCode #3392: Count Subarrays of Length Three With a Condition
function countSubarrays(nums: number[]): number {
let ans: number = 0;
for (let i = 1; i + 1 < nums.length; ++i) {
if ((nums[i - 1] + nums[i + 1]) * 2 === nums[i]) {
++ans;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.