#C11635. Find Two Sum Indices
Find Two Sum Indices
Find Two Sum Indices
You are given an array of integers and a target integer. Your task is to determine if any two distinct numbers in the array add up to the target value. If such a pair exists, output the indices (0-indexed) of these two numbers in ascending order (i.e., the smaller index first). If no such pair exists, output -1.
Note: There is exactly one solution or no solution. Each input is guaranteed to have at most one valid pair.
The formula for the problem can be summarized as follows:
Find indices i and j (with i < j) such that:
inputFormat
The input is read from stdin and consists of three lines:
- The first line contains an integer
n
denoting the number of elements in the array. - The second line contains
n
space-separated integers representing the elements of the array. - The third line contains an integer
target
, the target sum value.
outputFormat
Print the indices of the two numbers that add up to the target in ascending order, separated by a space. If no such pair exists, print -1
. The output should be written to stdout.
5
2 7 11 15 1
9
0 1