#C6303. Find Unique Pairs with Given Sum
Find Unique Pairs with Given Sum
Find Unique Pairs with Given Sum
You are given an array of integers and a target integer \( k \). Your task is to find all unique pairs of integers in the array such that their sum equals \( k \). A pair \( (a, b) \) is considered unique if \( a \leq b \), and duplicate pairs should not be included in the output.
The output must be sorted in ascending order. For example, if the pair \( (2, 7) \) is a valid result, then it should be printed as (2, 7)
, and the complete output for a test case should be represented in Python list format, such as [(2, 7)]
.
Hint: You can use sets to help ensure uniqueness. The mathematical formulation is: find all pairs \( (a, b) \) in the array such that \( a + b = k \) and \( a \leq b \).
inputFormat
The input is read from stdin and consists of multiple test cases. The first line contains an integer \( T \) indicating the number of test cases. For each test case, the input is structured as follows:
- An integer \( n \) representing the number of elements in the array.
- A line with \( n \) space-separated integers (if \( n > 0 \)); if \( n = 0 \), the array is empty.
- An integer \( k \) denoting the target sum.
outputFormat
For each test case, output a single line to stdout containing the list of unique pairs that sum up to \( k \). Each pair must be in the form (a, b)
and the overall format should match Python list notation. For example, if a valid output is a single pair, it should be printed as:
[(2, 7)]
If no such pairs exist, output an empty list:
[]## sample
3
5
2 7 11 15 1
9
4
1 5 3 3
6
3
3 4 8
10
[(2, 7)]
[(1, 5), (3, 3)]
[]
</p>