A conveyor belt in a packaging plant has a weight sensor mounted every meter along its length, and each sensor logs one integer reading as items pass beneath it, producing a sequence of n readings in order. Three consecutive sensors at positions i, i+1, and i+2 form a 'balance triplet' if the middle sensor's reading equals exactly twice the sum of the two outer sensors' readings. Count how many balance triplets occur among the n readings.
The first line contains one integer n. The second line contains n space-separated integers, the sensor readings in order.
A single integer: the number of indices i (0-indexed, 0 <= i <= n-3) such that reading[i+1] = 2 * (reading[i] + reading[i+2]).
Example 1
Input
5 3 1 4 1 5
Expected
1
Explanation
Check each window of three consecutive readings: (3,1,4) needs 1=2*(3+4)=14, false; (1,4,1) needs 4=2*(1+1)=4, true; (4,1,5) needs 1=2*(4+5)=18, false. Exactly one balance triplet is found, so the output is 1.
Example 2
Input
5 1 4 1 4 1
Expected
2
Explanation
The windows are (1,4,1): 4=2*(1+1)=4, true; (4,1,4): 1=2*(4+4)=16, false; (1,4,1): 4=2*(1+1)=4, true. Two of the three overlapping windows are balance triplets, so the output is 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →