#C6824. Find Pair Indices in an Array
Find Pair Indices in an Array
Find Pair Indices in an Array
You are given an array of n integers and a target sum k. Your task is to determine if there exist any two distinct elements in the array such that their sum equals k. In other words, find indices i and j (1-based) such that:
\(a_i + a_j = k\)
If such a pair exists, output the indices of any one such pair; otherwise, output -1 -1
. The input contains multiple test cases and terminates with a test case where both n and k are 0 (this terminating test case should not be processed).
Examples:
- For
n = 4, k = 9
and array[1, 2, 3, 7]
, one valid answer is2 4
because2 + 7 = 9
. - For
n = 5, k = 40
and array[20, 15, 30, 40, 25]
, one valid answer is2 5
because15 + 25 = 40
. - If no pair exists, output
-1 -1
.
inputFormat
The input consists of multiple test cases. Each test case is described with two lines:
- The first line contains two integers n and k, where n is the number of elements and k is the target sum.
- The second line contains n space-separated integers representing the array elements.
The input terminates with a test case where n and k are both 0. This test case should not be processed.
outputFormat
For each test case, output a single line containing two space-separated integers. These integers represent the 1-based indices of the two elements whose sum is k. If no such pair exists, output -1 -1
.
4 9
1 2 3 7
5 40
20 15 30 40 25
3 6
1 2 3
0 0
2 4
2 5
-1 -1
</p>