#C13568. Find Pairs with a Given Sum
Find Pairs with a Given Sum
Find Pairs with a Given Sum
You are given a list of integers and a target integer. Your task is to find all unique pairs of indices (i, j) such that:
[ numbers[i] + numbers[j] = target ]
Each element in the list may only be used once per pair, meaning that the same index cannot be repeated. The solution should be efficient in terms of time complexity and handle both positive and negative integers.
Example 1:
Input: [2, 7, 11, 15], target = 9 Output: (0, 1)
Example 2:
Input: [3, 2, 4, 3], target = 6 Output: (1, 2) and (0, 3)
inputFormat
The input is given via stdin
with the following format:
- The first line contains an integer
n
— the number of elements in the list. - The second line contains
n
space-separated integers representing the list. - The third line contains a single integer
target
— the sum you need to achieve.
outputFormat
Output the indices of each pair on a separate line in the format i j
, where i
and j
are the indices from the list such that numbers[i] + numbers[j] = target
. If no such pair exists, output nothing.
4
2 7 11 15
9
0 1
</p>