Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Explanation: Because nums[1] + nums[2] == 6, we return [1, 2].
Invariant: At step i, the Hash Table contains exactly the elements nums[0 ... i-1]. If a pair exists with nums[i], its complement has already been recorded in the map.
Hash tables convert time bottlenecks into linear space by indexing previous elements for O(1) queries.
Bottleneck: Brute force scans all N*(N-1)/2 pairs in O(N²). Hash table trades O(N) RAM to reduce lookup from O(N) to O(1).
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Explanation: Because nums[1] + nums[2] == 6, we return [1, 2].
Invariant: At step i, the Hash Table contains exactly the elements nums[0 ... i-1]. If a pair exists with nums[i], its complement has already been recorded in the map.
Hash tables convert time bottlenecks into linear space by indexing previous elements for O(1) queries.
Bottleneck: Brute force scans all N*(N-1)/2 pairs in O(N²). Hash table trades O(N) RAM to reduce lookup from O(N) to O(1).
Initialized empty Hash Table for O(1) complement lookups. Target sum = 9.