PRACTICE · ARRAYS & HASHING
Two Sum
The challenge
Given an array of integers and a target, return the indices of the two numbers that add up to the target. Each input has exactly one solution, and you may not use the same element twice.
Example
Input nums = [2, 7, 11, 15], target = 9
Output [0, 1]
The values at indices 0 and 1 add up to 9.
Before you begin
- Read through the example and consider edge cases.
- Write down a simple solution before optimizing.
- Describe the time and space complexity of your approach.
A space to understand the problem. Use your favorite editor to write and run your solution.