#C13668. Find Pairs with a Given Sum
Find Pairs with a Given Sum
Find Pairs with a Given Sum
Given a list of integers and a target sum \(S\), your task is to find all pairs \((a, b)\) such that \(a + b = S\). The pairs should be reported in the order they are found in the list. If no pairs exist that satisfy the condition, output the message "No pairs found that add up to the target sum."
Note: Use the optimal approach with a hash table (or similar data structure) to achieve an efficient solution.
For example, given the list [1, 2, 3, 4, 5]
and target sum 5
, one valid answer is [(2, 3), (1, 4)]
because \(2 + 3 = 5\) and \(1 + 4 = 5\).
inputFormat
The input is read from stdin
and consists of three lines:
- An integer
n
representing the number of elements in the list. - A line containing
n
space-separated integers. - An integer representing the target sum
S
.
outputFormat
If one or more pairs exist whose sum is equal to S
, output the list of pairs in the format [(a, b), (c, d), ...]
to stdout
. If no such pairs exist, output the exact string "No pairs found that add up to the target sum."
5
1 2 3 4 5
5
[(2, 3), (1, 4)]