A bell-tower technician records the resonance level of every chime mounted along a long rack, listed from left to right. Three chimes at positions i < j < k form a harmonious trio when the resonance gap between the first and second chime is at most a, the gap between the second and third chime is at most b, and the gap between the first and third chime is at most c -- where each gap is the absolute difference of the two chimes' resonance levels. Count how many harmonious trios of chime positions exist in the rack.
Line 1: four integers n, a, b, c. Line 2: n integers, the resonance levels of the chimes in rack order (0-indexed).
A single integer: the number of harmonious trios (i, j, k) with i < j < k satisfying all three gap conditions.
Example 1
Input
6 7 2 3 3 0 1 1 9 7
Expected
4
Explanation
Levels are [3,0,1,1,9,7] with a=7, b=2, c=3. Checking all C(6,3)=20 triples finds exactly 4 that satisfy every gap: (0,1,2) levels 3,0,1 with gaps 3,1,2; (0,1,3) levels 3,0,1 with gaps 3,1,2; (0,2,3) levels 3,1,1 with gaps 2,0,2; and (1,2,3) levels 0,1,1 with gaps 1,0,1. Every other triple involves the 9 or 7 at positions 4-5, whose gaps to the small values exceed one of the limits. Total is 4.
Example 2
Input
3 0 0 1 1 1 2
Expected
0
Explanation
The only triple is (0,1,2) with levels 1,1,2. The first gap |1-1|=0 is within a=0, but the second gap |1-2|=1 exceeds b=0, so the triple fails. Total is 0.
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 →