A wearable heart monitor logs the exact timestamp of every detected heartbeat, in strictly increasing order with no two beats sharing a timestamp. A calibration check considers three logged beats — taken in chronological order — to demonstrate a steady rhythm at a target interval gap if the time from the first beat to the second equals gap, and the time from the second beat to the third also equals gap. Count how many such triples of beats exist in the log (the three beats need not be consecutive entries in the log — only their timestamps must satisfy the two equal gaps).
The first line contains two integers n and gap — the number of logged beats and the target interval.
The second line contains n strictly increasing integers — the beat timestamps.
Print a single integer: the number of triples (i, j, k) with i < j < k such that timestamp[j] - timestamp[i] = gap and timestamp[k] - timestamp[j] = gap.
Example 1
Input
5 2 2 4 6 8 10
Expected
3
Explanation
With gap=2, the valid triples are (2,4,6), (4,6,8), and (6,8,10) — each of these has both gaps equal to exactly 2, giving three qualifying triples.
Example 2
Input
5 1 1 5 6 7 10
Expected
1
Explanation
With gap=1, only the triple (5,6,7) has both gaps equal to 1 (6-5=1 and 7-6=1); no other triple of timestamps satisfies both conditions, so the answer is 1.
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 →