#K37127. Two Sum
Two Sum
Two Sum
This problem is a classic challenge. Given an array of integers and a target integer \(T\), you are required to find two distinct elements in the array whose sum is equal to \(T\). If there are multiple solutions, return the first valid pair by the order of their appearance. If no such pair exists, output -1.
For example, if the input array is [2, 7, 11, 15] and \(T = 9\), the correct answer is 0 1
because \(2 + 7 = 9\).
inputFormat
The input is read from stdin
and has the following format:
- The first line contains an integer \(n\) representing the number of elements in the array.
- The second line contains \(n\) space-separated integers.
- The third line contains a single integer \(T\) representing the target sum.
outputFormat
If a valid pair exists, output the two indices (0-indexed) separated by a single space to stdout
. If no such pair exists, output -1.
4
2 7 11 15
9
0 1