Points on Line

Practice this coding question on Coding Gurukul.

Problem statement

Surveyors have placed markers at distinct positions along a straight track. A measurement team uses exactly three markers, and its equipment can cover them only when the distance between the farthest two does not exceed d. The coordinator wants to count every eligible team of markers. Your task Count triples of distinct indices i < j < k for which x[k] - x[i] <= d. The coordinates are already in strictly increasing order. Important details The three chosen markers need not be consecutive. Count an unordered selection once by using increasing indices. Negative coordinates are valid. A span equal to d is allowed. Use a 64-bit integer for the number of triples. Worked example For coordinates 0, 2, 3 and 6 with d = 4, the triples (0,2,3) and (2,3,6) qualify, giving 2. The other two triples span 6 units. With fewer than three markers, the answer is zero regardless of d.

Input format

Read n d, then n strictly increasing integer coordinates.

Output format

Print the number of valid triples.

Constraints

1 <= n <= 3000; 1 <= d <= 1000000000; -1000000000 <= x[i] <= 1000000000.

Sample input

4 4
0 2 3 6

Sample output

2

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…