#C13655. Two Sum Problem
Two Sum Problem
Two Sum Problem
Given an array of integers nums
and an integer target
, find the indices of the two numbers such that they add up to the target. More formally, find indices i and j (with i < j) satisfying the equation $$nums[i] + nums[j] = target.$$ If there is no such pair, output -1.
You may assume that each input would have at most one solution. The answer must be returned as two indices separated by a space in the order they appear in the array. This is a classic problem that tests your ability to use data structures such as hash maps for an efficient solution.
inputFormat
The input is read from standard input (stdin
) and consists of three lines:
- The first line contains an integer
n
representing the number of elements innums
. - The second line contains
n
space-separated integers representing the arraynums
. - The third line contains a single integer
target
.
outputFormat
Output to standard output (stdout
) the indices of the two numbers that add up to target
, separated by a space. If no such pair exists, output -1.
4
2 7 11 15
9
0 1