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.
Example 1:
Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6 Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6 Output: [0,1]
Constraints:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
O(n2)
time complexity?When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to this problem is all about trying every single combination. We check each number in the set against every other number to see if they add up to our target value. It's like exhaustively checking every possible pair until we find the right one.
Here's how the algorithm would work step-by-step:
def two_sum_brute_force(numbers, target):
# Iterate through each number in the list
for first_number_index in range(len(numbers)):
# Compare with every number after the current one
for second_number_index in range(first_number_index + 1, len(numbers)):
# Check if the current pair sums to target
if numbers[first_number_index] + numbers[second_number_index] == target:
return [first_number_index, second_number_index]
# No solution found.
return []
The efficient way to solve this problem involves remembering information as we go. Instead of checking every possible pair of numbers, we use a tool to quickly see if the number we need has already appeared.
Here's how the algorithm would work step-by-step:
def find_two_sum(number_list, target_sum):
number_index_map = {}
for index, number in enumerate(number_list):
needed_number = target_sum - number
# Check if the needed number exists in the lookup table
if needed_number in number_index_map:
return [number_index_map[needed_number], index]
# Store the current number and its index in the lookup
number_index_map[number] = index
return None
Case | How to Handle |
---|---|
Empty input array | Return an empty list or null, indicating no solution possible. |
Input array with only one element | Return an empty list or null, because two distinct numbers are required. |
Input array with two identical elements summing to target | The problem statement says all elements should be distinct. The problem does not define this. Throw an exception or return an error code if this is the case. |
Large input array size potentially causing memory issues with a hash map approach | Ensure the hash map implementation can handle the expected number of elements without exceeding memory limits; consider space-optimized data structures if necessary. |
Input array containing negative numbers | The hash map approach handles negative numbers correctly. |
Integer overflow if two large numbers sum to the target (language specific) | Use a data type that can accommodate the sum, or check for overflow before performing the addition. |
The target value is outside the possible sum range given the numbers in the array | The algorithm will not find a match and return an empty array or null, as expected |
Array contains duplicate values that could lead to incorrect index return if not handled carefully | Hashmap values will be overwritten which prevents us from accessing the same index twice. |