A ticket office keeps its list of n fares sorted in non-decreasing order. You are given a target amount T. Count how many index pairs (i, j) with i < j have fare[i] + fare[j] == T. Pairs are counted by index, so two tickets that happen to share the same fare value still count as separate pairs.
Line 1: two integers n and T.
Line 2: n space-separated integers, sorted in non-decreasing order — the fares.
A single integer: the number of index pairs (i, j), i < j, with fare[i] + fare[j] == T.
Example 1
Input
5 6 1 2 3 4 5
Expected
2
Explanation
The pairs (1,5) and (2,4) both sum to 6, so the count is 2.
Example 2
Input
4 4 2 2 2 2
Expected
6
Explanation
Every one of the C(4,2) = 6 index pairs sums to 2 + 2 = 4.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →