Title: Solving the 4-Sum Problem in JavaScript: A Step-by-Step Guide
When it comes to mastering Data Structures and Algorithms (DSA), solving complex problems like 4-Sum is essential. This problem involves finding all unique quadruplets (four numbers) in an array that sum up to a given target. It's an extension of the 2-Sum and 3-Sum problems, but with added complexity due to the additional combinations to consider.
In this article, we'll break down the 4-Sum problem and walk through an efficient solution using JavaScript. We’ll also explain the code in detail so that you can implement it with ease.
Problem Statement:
You are given an array nums of integers and a target integer. Your goal is to return all unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
Example:
Input:
nums = [1, 0, -1, 0, -2, 2], target = 0
Output:
[[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]
Input:
nums = [2, 2, 2, 2, 2], target = 8
Output:
[[2, 2, 2, 2]]
Approach to Solve the Problem:
The 4-Sum problem can be tackled with a similar strategy to 3-Sum, but with one extra loop to account for the fourth number. Here’s how you can approach it step-by-step:
Step 1: Sort the Array
Sorting the array helps in avoiding duplicates and also allows us to efficiently use the two-pointer technique later.
Step 2: Use Nested Loops
We will use two loops to iterate over the array and fix two numbers at a time. After fixing two numbers, we will use two pointers (left and right) to find the remaining two numbers whose sum matches the target.
Step 3: Skip Duplicates
Since we are asked to find unique quadruplets, we need to ensure that we skip over duplicate numbers in the array.
Step 4: Optimize with Two Pointers
By using two pointers (one starting from the left and one from the right), we can reduce the time complexity, making our solution more efficient.
JavaScript Solution:
var fourSum = function(nums, target) {
// Step 1: Sort the array
nums.sort((a, b) => a - b);
let result = [];
// Step 2: First two loops to fix the first two numbers
for (let i = 0; i < nums.length - 3; i++) {
// Skip duplicate numbers for the first element
if (i > 0 && nums[i] === nums[i - 1]) continue;
for (let j = i + 1; j < nums.length - 2; j++) {
// Skip duplicate numbers for the second element
if (j > i + 1 && nums[j] === nums[j - 1]) continue;
领英推荐
// Step 3: Use two pointers to find the other two numbers
let left = j + 1;
let right = nums.length - 1;
while (left < right) {
let sum = nums[i] + nums[j] + nums[left] + nums[right];
// If the sum is less than the target, move the left pointer to the right
if (sum < target) {
left++;
}
// If the sum is greater than the target, move the right pointer to the left
else if (sum > target) {
right--;
}
// If the sum equals the target, we have found a valid quadruplet
else {
result.push([nums[i], nums[j], nums[left], nums[right]]);
// Skip duplicate numbers for the third and fourth elements
while (left < right && nums[left] === nums[left + 1]) left++;
while (left < right && nums[right] === nums[right - 1]) right--;
// Move the pointers to continue searching for other pairs
left++;
right--;
}
}
}
}
// Step 4: Return the result array containing all unique quadruplets
return result;
};
Explanation:
Time and Space Complexity:
Conclusion:
The 4-Sum problem is a great exercise in improving your understanding of both nested loops and the two-pointer technique. By breaking the problem down into steps, using sorting, and efficiently skipping duplicates, we arrive at an optimal solution. Practicing problems like this on platforms like LeetCode will sharpen your problem-solving skills and make you more confident in tackling algorithmic challenges.
Call to Action: Have you solved any variations of the 4-Sum problem? Feel free to share your experiences or alternative solutions in the comments below!