All problems

Problem 309

Four Sum Count

Medium · 20 pts · Array, Two Pointers, Sorting

Problem

Count the distinct quadruplets of values (a, b, c, d) taken from four different positions of the array such that a + b + c + d equals the target T. Quadruplets are compared by value: two quadruplets that use the same four values (in any order) count once, even if they come from different positions. This extends the classic 3Sum idea with one more nested loop.

Input

The first line contains N and T. The second line contains N integers.

Output

Print the number of distinct quadruplets.

Constraints

4 <= N <= 200; -1000000000 <= A[i], T <= 1000000000

Example input

6 0
1 0 -1 0 -2 2

Example output

3

Explanation

With T = 0 in 1 0 -1 0 -2 2, the distinct quadruplets are (-2, -1, 1, 2), (-2, 0, 0, 2) and (-1, 0, 0, 1), so the answer is 3.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Count Triplets with Smaller Sum Next: Majority Element II