#K92762. Two Sum Problem
Two Sum Problem
Two Sum Problem
Given an array of integers and a target integer \(T\), find two numbers from the array such that their sum equals \(T\). The answer should be output in the order in which the numbers appear: the first output number must be the one that appeared earlier in the array. If there is no such pair, output -1
.
For example, if the input is 2 7 11 15
with \(T = 9\), then the expected output is 2 7
.
inputFormat
The input is read from standard input and consists of three lines:
- 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 elements.
- The third line contains an integer \(T\), the target sum.
outputFormat
If a valid pair exists such that their sum equals \(T\), output the two integers on one line separated by a space. Otherwise, output -1
.
4
2 7 11 15
9
2 7