Count Unique Element PairsMedium

Count Unique Element Pairs

MediumPractice library · +20 XPhash maptwo pointerssorting

Given an array of N integers, find the number of distinct pairs (i, j) such that i < j and arr[i] + arr[j] = X, for a given integer X. Two pairs (i, j) and (k, l) are distinct if {i, j} != {k, l}.

Input

The first line contains two integers N and X, where N is the size of the array and X is the target sum. The second line contains N space-separated integers arr[0], arr[1], ..., arr[N-1].

Output

Print a single integer: the count of distinct pairs (i, j) with i < j and arr[i] + arr[j] = X.

Constraints

1 <= N <= 200000 -10^9 <= arr[i] <= 10^9 -210^9 <= X <= 210^9

Time limit 1 s (native code; slower languages get more)Memory 256 MB16 hidden tests

Example 1

Input
5 7
1 5 3 4 2
Output
2

Pairs summing to 7 are (3, 4), (5, 2). Count is 2.

Example 2

Input
6 10
2 2 4 6 8 8
Output
5

Pairs summing to 10 are (2, 8), (2, 8), (4, 6). Note that the two 8s with the first 2 form two pairs, and the 6 with the 4 forms one pair. Count is 3.

Hints