#K58312. Two Sum: Find Indices That Add Up to Target
Two Sum: Find Indices That Add Up to Target
Two Sum: Find Indices That Add Up to Target
Given a list of integers and a target integer \(T\), your task is to find the indices of the two numbers in the list that add up exactly to \(T\). If no such pair exists, output an empty list []
. In case multiple valid pairs exist, return the one with the smallest indices (i.e. the first encountered pair in a left-to-right scan).
Example:
For the list [2, 7, 11, 15]
and target 9
, the answer is [0, 1]
since \(2+7=9\).
The problem can be formulated as: find indices \(i, j\) such that:
[ \text{nums}[i] + \text{nums}[j] = T, \quad i < j ]
If there is no solution, return an empty list []
.
inputFormat
The input is read from stdin and consists of two lines:
- The first line contains a series of integers separated by spaces, representing the list of numbers.
- The second line contains a single integer, the target \(T\).
For example:
2 7 11 15 9
outputFormat
The output should be printed to stdout on a single line. If a valid pair is found, print the two indices separated by a space. If no such pair exists, print []
(without quotes).
For example:
0 1## sample
2 7 11 15
9
0 1