#K5531. Find Pairs with Given Sum

    ID: 29947 Type: Default 1000ms 256MiB

Find Pairs with Given Sum

Find Pairs with Given Sum

Given a list of integers and a target sum \(S\), find all unique pairs \((a, b)\) such that \(a \le b\) and \(a+b=S\). Each pair should appear only once and the output should be sorted in increasing order (first by \(a\) then by \(b\)).

For example, if the input list is "1 2 3 4 5" and the target sum is 5, the valid pairs are [(1, 4), (2, 3)].

inputFormat

The input consists of two lines:

  • The first line contains space-separated integers representing the list of numbers.
  • The second line contains a single integer representing the target sum \(S\).

outputFormat

Output a single line showing the list of unique pairs, in the Python list format. Each pair is printed as a tuple \((a, b)\). For instance, if no pairs can be found, output [].

## sample
1 2 3 4 5
5
[(1, 4), (2, 3)]