#C8634. Count Unique Triplets

    ID: 52638 Type: Default 1000ms 256MiB

Count Unique Triplets

Count Unique Triplets

You are given an array of N integers and an integer S. Your task is to find the number of unique triplets (i, j, k) such that 0 ≤ i < j < k < N and the sum of the three elements equals S. Formally, you need to count the number of triplets satisfying

$$a_i + a_j + a_k = S$$

A triplet is considered unique if the indices i, j, k are distinct, and two triplets with the same set of values (in sorted order) are counted as one. An efficient way to solve this problem is to sort the array and then use a two-pointer approach to identify valid triplets.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains a single integer T, the number of test cases.
  • For each test case:
    • The first line contains two integers N and S, where N is the number of elements in the array.
    • The second line contains N space-separated integers which represent the array.

outputFormat

For each test case, output a single integer on a new line representing the number of unique triplets (i, j, k) such that the sum of the elements equals S. The output should be written to standard output (stdout).

## sample
3
5 9
1 2 3 4 5
4 0
-1 0 1 2
3 3
1 1 1
2

1 1

</p>