#K37812. Find Pairs with Given Sum

    ID: 26060 Type: Default 1000ms 256MiB

Find Pairs with Given Sum

Find Pairs with Given Sum

Given an array of integers \(a_1, a_2, \ldots, a_N\) and a target sum \(K\), find all pairs of numbers \((a_i, a_j)\) (where the pair is formed as soon as the second number is encountered) such that \(a_i + a_j = K\). Note that an element may be used in more than one pair if it appears multiple times. Two pairs are considered distinct if their occurrences differ, even if the numbers are the same.

Example: For the array [1, 5, 7, -1, 5] with \(K=6\), the valid pairs are:

  • (1, 5) from elements 1 and the first 5
  • (7, -1) from elements 7 and -1
  • (1, 5) from elements 1 and the second 5

The pairs should be printed in the order they are found.

inputFormat

The input is read from standard input (stdin) and consists of:

  1. An integer \(N\), representing the number of elements in the array.
  2. A line with \(N\) space-separated integers.
  3. An integer \(K\), representing the target sum.

outputFormat

Output each valid pair on a separate line. Each line should contain two space-separated integers representing the pair. If no valid pairs exist, output nothing.

## sample
5
1 5 7 -1 5
6
1 5

7 -1 1 5

</p>