Count Triplets!

5

1 votes
Binary Search
Problem

Chuchu has got an array of size n, now he wants to count number of unordered triplets present in the array such that absolute difference between any two elements of a triplet should be at least d. Formally, if (a,b,c) is a triplet then |ab|d,|bc|d and |ca|d.

Help Chuchu in counting such triplets.

Input:

  • First line contains a integer T , denoting number of test cases.
  • Each test case consists of two lines, first line contains two integers n and d and second line  contains n space separated integers, denoting array elements.

Output:

For each query output the number of triplets satisfying above mentioned condition in a newline.

Constraints:

  • 1T5
  • 1n105
  • 1Ai,d109
Sample Input
2
3 2
2 4 6
4 1
1 2 3 4
Sample Output
1
4
Time Limit: 1.5
Memory Limit: 256
Source Limit:
Explanation

For first test case [2,4,6], triplet (2,4,6) follows the given criteria, so answer is 1.

Editor Image

?