#K34812. Two Sum Problem
Two Sum Problem
Two Sum Problem
Given an array of integers and a target value, your task is to find the indices of the two numbers such that their sum equals the target. In other words, find indices \(i\) and \(j\) (with \(i \neq j\)) satisfying
$$ nums[i] + nums[j] = \text{target} $$
If a valid pair exists, output the two indices (0-indexed) separated by a space. If no such pair exists, output []
.
You can assume that each test input will have exactly one solution, although some test cases may have no valid pair.
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 in the array.
- The second line contains \(n\) space-separated integers representing the array elements.
- The third line contains an integer representing the target sum.
outputFormat
If a valid pair is found, print the two indices (0-indexed) separated by a space. If no valid pair exists, print []
.
4
2 7 11 15
9
0 1