problems / 247

Counting Good Triplets

easy brute-force arrays 1000 ms · 256 MB

Counting Good Triplets

You are given an array a_1, a_2, …, a_n of integers and three bounds x, y and z.

A triple of indices (i, j, k) with 1 ≤ i < j < k ≤ n is good if all three of these hold:

  • |a_i − a_j| ≤ x
  • |a_j − a_k| ≤ y
  • |a_i − a_k| ≤ z

Count the good triples. Triples are counted by their indices, so equal values at different positions give different triples.

Input

The first line contains t (1 ≤ t ≤ 100), the number of test cases.

Each test case consists of two lines. The first line contains four integers n, x, y, z (1 ≤ n ≤ 100, 0 ≤ x, y, z ≤ 1000). The second line contains n integers a_1, …, a_n (0 ≤ a_i ≤ 1000).

The sum of n over all test cases does not exceed 500.

Output

For each test case, print one line with the number of good triples.

Notes

In the first test case of the first example, the array is 3 0 1 1 9 7 with x = 7, y = 2, z = 3. The good triples are (1,2,3), (1,2,4), (1,3,4) and (2,3,4), with values (3,0,1), (3,0,1), (3,1,1) and (0,1,1). In the second test case no triple is good. In the third test case n = 2, so there are no triples at all and the answer is 0.

In the second example every triple of an all-equal array is good, so the answer is the number of ways to pick three of the five positions.

Example 1

Input

3
6 7 2 3
3 0 1 1 9 7
5 0 0 1
1 1 2 2 3
2 5 5 5
4 4

Output

4
0
0

Example 2

Input

1
5 0 0 0
7 7 7 7 7

Output

10

Hints · reveal one at a time if you're stuck

Hint 1

Look at the limits: how many triples i < j < k can an array of 100 elements have? Is that too many to check one by one?

Hint 2

At most about 1.6·10^5 triples per test, and the sum of n is at most 500, so trying every triple is fast enough. Don't sort the array: each bound belongs to a specific pair of positions, and the pair (i, k) has its own condition too.

report an issue Prefer your own editor? sooks prep 247 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.