#K62417. Two Sum Problem

    ID: 31527 Type: Default 1000ms 256MiB

Two Sum Problem

Two Sum Problem

Given an array of integers and a target value, your task is to find indices of the two numbers such that they add up to the target. You may assume that each input has exactly one solution, or in cases where no valid pair exists, output an empty list []. The indices should be zero-based.

For example, if the given array is [2, 7, 11, 15] and the target is 9, then the two numbers 2 and 7 (at indices 0 and 1) add up to 9, so the answer is "0 1".

Note: You must read input from standard input (stdin) and write your output to standard output (stdout).

The problem can be formally defined using the following equation in LaTeX: $$\text{nums}[i] + \text{nums}[j] = \text{target}\quad,\quad i \neq j$$

inputFormat

The input is given in the following format via stdin:

n
num_0 num_1 ... num_(n-1)
target

Where:

  • n is an integer representing the number of elements in the array.
  • num_0, num_1, ..., num_(n-1) are the integers in the array separated by spaces.
  • target is the target integer value.

outputFormat

If a solution exists, output the two indices separated by a space. If there is no solution, output [] (without quotes). All output should be printed to stdout.

## sample
4
2 7 11 15
9
0 1