Number Pairs
InterviewTime limit2sMemory limit512 MB
Count pairs of distinct values in a sequence that sum to a target K, where order does not matter and each element is used once.
- Level
Easy3 of 10
- Topics
- Array, Hash map, Two pointers, Sorting
- Solved
- No attempts yet
Problem
You are given a sequence of distinct integers. Count how many pairs of elements in the sequence add up to .
A pair is made of two elements at different positions, and the order inside a pair does not matter, so and are the same pair. The same element cannot be used twice, so when is even and appears in the sequence, that single value does not form a pair by itself.
For the sequence and the pairs , , satisfy the condition, so the answer is . For the same sequence and the pairs and satisfy it, so the answer is .
Input
The first line contains the sequence length and the target sum , separated by a space.
The second line contains the elements of the sequence, separated by spaces.
. The elements are pairwise distinct integers between and . is an integer between and .
Output
Print one integer, the number of pairs whose sum is .