#K49012. Two Sum Indices
Two Sum Indices
Two Sum Indices
You are given an array of integers and a target integer. Your task is to find two distinct indices in the array such that the sum of the elements at those indices equals the target. If such a pair exists, print the indices (the first occurrence in the array) separated by a space; otherwise, print -1 -1
.
Note: Each input test case starts with two numbers: the size of the array n
and the target value, followed by n
integers representing the array elements.
The solution should use an efficient algorithm, for example by using a hash map, so that it works well even for large arrays.
In mathematical notation, if the array is represented as \(a_0, a_1, \dots, a_{n-1}\), find indices \(i\) and \(j\) such that: \[ a_i + a_j = target, \quad i \neq j. \] If no such indices exist, output \(-1\) for both indices.
inputFormat
The input is read from standard input and consists of:
- The first line contains two integers:
n
(the number of elements in the array) andtarget
. - The second line contains
n
space-separated integers which are the elements of the array.
outputFormat
Print a single line containing two space-separated integers. These integers are the indices of the two numbers in the array that add up to the target. If no such pair exists, print -1 -1
.
4 9
2 7 11 15
0 1