Skip to main content

4Sum

Problem Statement

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 < n
  • a, b, c, and d are distinct.
  • nums[a] + nums[b] + nums[c] + nums[d] == target

You 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
  • -1e9 <= nums[i] <= 1e9
  • -1e9 <= target <= 1e9

Code

Python Code
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
nums.sort()
n = len(nums)
ans = set()
for i in range(n):
for j in range(i+1, n):
l, r = j + 1, n - 1
remain = target - nums[i] - nums[j]
while l < r:
if nums[l] + nums[r] == remain:
ans.add((nums[i], nums[j], nums[l], nums[r]))
l += 1
r -= 1
elif nums[l] + nums[r] > remain:
r -= 1
else:
l += 1
return ans


C++
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int n = nums.size();
vector<vector<int>> ans;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
int l = j + 1, r = n - 1;
int remain = target - nums[i] - nums[j];
while (l < r) {
if (nums[l] + nums[r] == remain) {
ans.push_back({nums[i], nums[j], nums[l], nums[r]});
++l; --r;
while (l < r && nums[l-1] == nums[l]) ++l; // Skip duplicate nums[l]
} else if (nums[l] + nums[r] > remain) {
--r;
} else {
++l;
}
}
while (j+1 < n && nums[j] == nums[j+1]) ++j; // Skip duplicate nums[j]
}
while (i+1 < n && nums[i] == nums[i+1]) ++i; // Skip duplicate nums[i]
}
return ans;
}
};