#C6238. Two Sum Problem (Standard I/O Version)
Two Sum Problem (Standard I/O Version)
Two Sum Problem (Standard I/O Version)
You are given an array of integers and a target integer. Your task is to find the indices of the two numbers in the array such that they add up to the target. If there exists such a pair, output the two indices separated by a space. Otherwise, output []
to indicate that no valid pair exists.
Note: There is exactly one solution in the original problem statement, but in this version, there might be no solution. Use an efficient algorithm to achieve a time complexity close to \(O(n)\).
Examples:
- Input:
4\n2 7 11 15\n9
— Output:0 1
- Input:
4\n1 2 3 4\n10
— Output:[]
inputFormat
The input is read from standard input (stdin) and has the following format:
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 \(target\), the target sum.
outputFormat
The output should be written to standard output (stdout). If there exists a pair of indices \(i\) and \(j\) (with \(i < j\)) such that the sum \(nums[i] + nums[j] = target\), print the indices separated by a single space. If no such pair exists, print []
.
4
2 7 11 15
9
0 1