#K78937. Pairs with Sum K
Pairs with Sum K
Pairs with Sum K
You are given an array of integers A and a target sum K. Your task is to find all unique pairs (x, y) from the array such that \( x + y = K \). For each valid pair, print the pair in ascending order (i.e. the smaller number first), and ensure that no pair is repeated.
For example, if A = [1, 5, 7, -1, 5]
and K = 6
, the valid pairs are [-1, 7]
and [1, 5]
.
The input is given via standard input and the output should be printed to standard output, displaying one pair per line (two integers separated by a space). If no pair exists that sums to \( K \), do not print any output.
inputFormat
The input is provided via standard input in the following format:
- The first line contains an integer
n
representing the number of elements in the array. - The second line contains
n
space-separated integers representing the arrayA
. - The third line contains an integer
K
, the target sum.
outputFormat
For each unique pair (x, y) such that x + y = K
(with x ≤ y
), print a line with the two numbers separated by a single space. The pairs should be printed in the order they are found using the two-pointer technique after sorting the array. If no such pair exists, output nothing.
5
1 5 7 -1 5
6
-1 7
1 5
</p>