Ctrl K

Middle Twice Its Neighbors

Easy
You are given an integer array values. Look at every window of three consecutive elements. A window is good if the sum of its two outer elements is exactly half of the middle one. Return the number of good windows.
Example 1
values=[3, 2, 10, 3, 0] → 1

In [2, 10, 3] the ends add up to 5, exactly half of 10. In [10, 3, 0] the ends add up to 10 while half of 3 is 1.5. The answer is 1.

Example 2
values=[-1, -4, -1] → 1

-1 + (-1) = -2, which is half of -4.

Example 3
values=[4, 4, 4] → 0

The ends add up to 8, while half of the middle is 2.

Constraints
  • 3 ≤ len(values) ≤ 100
  • -100 ≤ values[i] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasySqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasySlot on a Sorted Shelf EasyAdd One to a Digit Array Easy
loading Python…
Input
values=[3, 2, 10, 3, 0]
Expected
1

Run checks the first 3 tests in your browser. Submit checks all 123 tests on the server, hidden ones included.