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 <= 109O(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 method for Two Sum involves checking every single possible pair of numbers in the given collection to see if they add up to the target number. It's like trying every combination until you find the right one.
Here's how the algorithm would work step-by-step:
def two_sum_brute_force(numbers, target_sum): number_count = len(numbers) # We need to iterate through each number in the list to consider it as the first element of a potential pair. for first_element_index in range(number_count): # For each chosen first element, we must check it against all subsequent elements to form unique pairs. for second_element_index in range(first_element_index + 1, number_count): # This check is crucial to see if the current pair sums up to the desired target. if numbers[first_element_index] + numbers[second_element_index] == target_sum: return [first_element_index, second_element_index] # If no pair is found after checking all combinations, it signifies that no solution exists. return []Instead of checking every single number against every other number, we can use a clever trick to quickly find if the 'missing' number needed to reach our target exists. This involves remembering the numbers we've already seen so we can instantly look them up later.
Here's how the algorithm would work step-by-step:
def find_two_sum_indices(numbers_list, target_value):
seen_numbers_map = {}
for current_index, current_number in enumerate(numbers_list):
complement_needed = target_value - current_number
# We need to check if the complement has been seen before to form a pair.
if complement_needed in seen_numbers_map:
return [seen_numbers_map[complement_needed], current_index]
# Store the current number and its index for future lookups.
seen_numbers_map[current_number] = current_index
# If no pair is found after checking all numbers.
return []| Case | How to Handle |
|---|---|
| Empty or null input array | An empty or null input array should result in an empty list being returned as no pair can be found. |
| Array with fewer than two elements | If the array has less than two elements, it's impossible to find two distinct numbers, so an empty list should be returned. |
| No pair sums to the target | The algorithm should iterate through the entire array and if no pair is found, it should return an empty list. |
| Multiple pairs sum to the target | The problem statement implies returning any valid pair, so the first one found is acceptable. |
| Array contains duplicate numbers | The hash map approach correctly handles duplicates by storing and checking against previous elements; we must ensure we don't use the same element twice by checking if the complement exists and is not the current element's index. |
| Target is exactly twice one of the numbers | The solution must ensure it doesn't return the index of the same element twice; checking if the complement's index is different from the current index is crucial. |
| Array contains negative numbers and zero | Negative numbers and zeros are handled correctly by the arithmetic operations and the hash map lookup. |
| Large input array (scalability) | A hash map approach provides O(n) time complexity which scales efficiently for large arrays. |