Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
0 <= a, b, c, d < na, b, c, and d are distinct.nums[a] + nums[b] + nums[c] + nums[d] == targetYou may return the answer in any order.
Example 1:
Input: nums = [1,0,-1,0,-2,2], target = 0 Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2:
Input: nums = [2,2,2,2,2], target = 8 Output: [[2,2,2,2]]
Constraints:
1 <= nums.length <= 200-109 <= nums[i] <= 109-109 <= target <= 109When 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 finding four numbers that add up to a target sum is to systematically check every single combination of four numbers from the given collection. We'll go through all possible groups of four and see if their sum matches what we're looking for.
Here's how the algorithm would work step-by-step:
def four_sum_brute_force(numbers, target_sum): found_quadruplets = [] number_count = len(numbers) # We need four distinct numbers, so we iterate up to number_count - 3 for the first number for first_index in range(number_count - 3): # The second number must come after the first, so we start from first_index + 1 for second_index in range(first_index + 1, number_count - 2): # The third number must come after the second for third_index in range(second_index + 1, number_count - 1): # The fourth number must come after the third for fourth_index in range(third_index + 1, number_count): current_sum = (numbers[first_index] + numbers[second_index] + numbers[third_index] + numbers[fourth_index]) # Check if the sum of the four chosen numbers equals the target sum if current_sum == target_sum: quadruplet = [numbers[first_index], numbers[second_index], numbers[third_index], numbers[fourth_index]] found_quadruplets.append(quadruplet) return found_quadrupletsThe most efficient way to find all unique combinations of four numbers that add up to a target sum involves a process of elimination and clever searching. We'll sort the numbers first, which is a key step to avoiding duplicate combinations and speeding up the search.
Here's how the algorithm would work step-by-step:
def find_four_sum_combinations(sorted_numbers, target_sum_quadruplet):
quadruplets = []
numbers_count = len(sorted_numbers)
if numbers_count < 4:
return quadruplets
sorted_numbers.sort()
# Iterate through the first number of the potential quadruplet.
for first_number_index in range(numbers_count - 3):
# Skip duplicate first numbers to avoid redundant combinations.
if first_number_index > 0 and sorted_numbers[first_number_index] == sorted_numbers[first_number_index - 1]:
continue
# Iterate through the second number of the potential quadruplet.
for second_number_index in range(first_number_index + 1, numbers_count - 2):
# Skip duplicate second numbers.
if second_number_index > first_number_index + 1 and sorted_numbers[second_number_index] == sorted_numbers[second_number_index - 1]:
continue
remaining_target_sum = target_sum_quadruplet - sorted_numbers[first_number_index] - sorted_numbers[second_number_index]
third_number_index = second_number_index + 1
fourth_number_index = numbers_count - 1
# Use two pointers to find the remaining two numbers efficiently.
while third_number_index < fourth_number_index:
current_pair_sum = sorted_numbers[third_number_index] + sorted_numbers[fourth_number_index]
if current_pair_sum == remaining_target_sum:
quadruplets.append([
sorted_numbers[first_number_index],
sorted_numbers[second_number_index],
sorted_numbers[third_number_index],
sorted_numbers[fourth_number_index]
])
# Skip duplicate third numbers.
while third_number_index < fourth_number_index and sorted_numbers[third_number_index] == sorted_numbers[third_number_index + 1]:
third_number_index += 1
# Skip duplicate fourth numbers.
while third_number_index < fourth_number_index and sorted_numbers[fourth_number_index] == sorted_numbers[fourth_number_index - 1]:
fourth_number_index -= 1
third_number_index += 1
fourth_number_index -= 1
elif current_pair_sum < remaining_target_sum:
third_number_index += 1
else:
fourth_number_index -= 1
return quadruplets| Case | How to Handle |
|---|---|
| Input array is null or empty | The solution should return an empty list as no quadruplets can be formed. |
| Input array has fewer than 4 elements | If the array size is less than 4, return an empty list immediately because a quadruplet cannot be formed. |
| Array contains all identical values | The solution must correctly identify all unique quadruplets, even if multiple combinations use the same identical values, by carefully managing indices and skipping duplicates. |
| Array contains negative numbers and zeros | The algorithm should handle sums involving negative numbers and zeros correctly without any special modifications. |
| The target value is very large or very small, potentially leading to integer overflow | Use of 64-bit integers (long long in C++, long in Java) for sum calculations will prevent overflow issues. |
| Multiple valid quadruplets exist | The solution must find and return all unique quadruplets, ensuring no duplicates in the output list. |
| No valid quadruplets sum to the target | The algorithm naturally handles this by returning an empty list if no combinations satisfy the condition. |
| Large input array size | The chosen approach, typically sorting followed by a k-sum generalization, should be efficient enough to pass within time limits, often O(n^3) or O(n^4) depending on implementation details. |