#C5389. Two Sum Indices
Two Sum Indices
Two Sum Indices
Given an array of integers and a target value \(T\), your task is to find two distinct indices \(i\) and \(j\) (with \(i < j\)) such that \(nums[i] + nums[j] = T\). If such a pair exists, output the indices separated by a space. Otherwise, output -1.
Note: The solution must read from stdin
and write the result to stdout
.
For example:
- Input:
5\n2 7 11 15 1\n9
→ Output:0 1
- Input:
4\n1 2 3 4\n8
→ Output:-1
inputFormat
The input is given on standard input (stdin
) in the following format:
- The first line contains an integer \(n\), the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the array \(nums\).
- The third line contains an integer \(T\), the target sum.
outputFormat
Print to standard output (stdout
) the two indices \(i\) and \(j\) (where \(i < j\)) separated by a space if a valid pair exists. Otherwise, print -1
.
5
2 7 11 15 1
9
0 1
</p>