Two turnstiles at a venue each stamp a signed integer offset on every entry, and the log lists n such offsets in the order they were recorded. Management wants to know how many pairs of distinct log entries have offsets that add up to exactly a target value t.
Count the number of unordered pairs of positions (i, j) with i < j such that entry[i] + entry[j] == t. The log is not sorted, and values may repeat or be negative.
Line 1: an integer n.
Line 2: n space-separated integers, the offsets in recorded order.
Line 3: an integer t, the target sum.
A single integer: the number of unordered index pairs whose offsets sum to t.
Example 1
Input
6 1 2 3 4 5 6 7
Expected
3
Explanation
The pairs that sum to 7 are (1,6), (2,5), and (3,4): three pairs.
Example 2
Input
5 2 2 2 2 2 4
Expected
10
Explanation
Every value is 2, so any two of the five entries sum to 4. There are 5 choose 2 = 10 such pairs.
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 →