JuniorArray

Count Subarrays of Length Three With a Condition

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
Inputnums = [1,2,1,4,1]
Output1
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
Inputnums = [1,1,1]
Output0
[1,1,1] is the only subarray of length 3, but its first and third numbers do not add to half the middle number.

Constraints

  • 3 <= nums.length <= 100
  • -100 <= nums[i] <= 100

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate