# Question

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

# Solution

## EN

The approach is using hash map to solve the problem. While iterating through the array, the value and corresponding index is stored into hash map. Whilst at the same time, the complementary value of the current index is also checked. If the value of complementary value exists in hash map, just directly return the index of complementary value and current index.

## Code

### Python3

For more enumerate, checkout Enumerate() in Python

# Complexity Analysis

• Time Complexity: $O(n)$ since only need to traverse the whole list containing $n$ element once; and the look up in the table costs $O(1)$ time.

• Space Complexity: $O(n)$. At the worst case, if the pair of index does not exist, then all elements are required to be stored in the hash map, which at most is $n$ elements.

Solution