Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Count Subarrays of Length Three With a Condition

XPChallenge Points: 10
levelLevel: Easy

You are given an integer array nums.
Your task is to count how many continuous subarrays of length 3 satisfy the following condition:

For a subarray [a, b, c], the sum of the first and third elements must be exactly half of the middle element, i.e.:

a +c= b /2


Return the total count of such valid subarrays.

Example 1:

Input: nums = [2, 8, 2, 4]

Output: 1

Explanation:

The subarray [2, 8, 2] is valid because 2 + 2 = 4 which equals 8/2.

Example 2:

Input: nums = [3, 6, 3, 6, 3]

Output: 0

Explanation:

Two valid subarrays are [3, 6, 3] appearing twice.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result