Choose points (Contest)
Choose points easy Time Limit: 2 sec Memory Limit: 128000 kB
Last updated
Choose points easy Time Limit: 2 sec Memory Limit: 128000 kB
Last updated
Problem Statement :
You are given N points on a horizontal axis. Find the number of ways you can choose 3 points such that maximum distance between those points is not greater than d. Input The first line contains two integers: N and d. The next line contains N space-separated integers x1,āx2,ā...,āxN, ā the x-coordinates of the points that Petya has got.
Constraints: 1āā¤āN ā¤ā1e5 1āā¤ādāā¤ā1e9 1 ⤠|x[i]| ⤠1e9
It is guaranteed that the coordinates of the points in the input strictly increase. Output Print the number of ways to choose 3 points. Example Sample Input: 4 3 1 2 3 4
Sample Output: 4
Explanation: 1 2 3 1 2 4 2 3 4 1 3 4 are the required triplets
Sample Input: 4 2 -3 -2 -1 0
Sample Output: 2
link: