|
| 1 | +--- |
| 2 | +id: FourSum |
| 3 | +title: 4Sum (LeetCode) |
| 4 | +sidebar_label: 0018-FourSum |
| 5 | +tags: |
| 6 | + - Two Pointers |
| 7 | +description: "Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]]" |
| 8 | +--- |
| 9 | + |
| 10 | +## Problem Description |
| 11 | + |
| 12 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 13 | +| :------------------------------------------------------ | :------------------------------------------------------------------------- | :------------------------------------------------------ | |
| 14 | +| [4Sum](https://leetcode.com/problems/4sum/description/) | [4Sum Solution on LeetCode](https://leetcode.com/problems/4sum/solutions/) | [Abhinash Singh](https://leetcode.com/u/singhabhinash/) | |
| 15 | + |
| 16 | +### Problem Description |
| 17 | + |
| 18 | +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: |
| 19 | + |
| 20 | +- `0 <= a, b, c, d < n` |
| 21 | +- `a, b, c, and d are distinct` |
| 22 | +- `nums[a] + nums[b] + nums[c] + nums[d] == target` |
| 23 | + |
| 24 | +You may return the answer in any order. |
| 25 | + |
| 26 | +### Examples |
| 27 | + |
| 28 | +#### Example 1 |
| 29 | + |
| 30 | +- **Input:** `nums = [1,0,-1,0,-2,2], target = 0` |
| 31 | +- **Output:** `[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]` |
| 32 | + |
| 33 | +#### Example 2 |
| 34 | + |
| 35 | +- **Input:** `nums = [2,2,2,2,2], target = 8` |
| 36 | +- **Output:** `[[2,2,2,2]]` |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- $1 <= nums.length <= 200$ |
| 41 | +- $-10^9 <= nums[i] <= 10^9$ |
| 42 | +- $-10^9 <= target <= 10^9$ |
| 43 | + |
| 44 | +### Approach |
| 45 | + |
| 46 | +To solve the problem, we can use the following approach: |
| 47 | + |
| 48 | +1: Sort the input array in non-decreasing order. |
| 49 | +2: Traverse the array from 0 to n-3 and use a variable i to keep track of the first element in the quadruplet. |
| 50 | +3: If the current element is the same as the previous element, skip it to avoid duplicates. |
| 51 | +4: Traverse the array from i+1 to n-2 and use a variable j to keep track of the second element in the quadruplet. |
| 52 | +5: If the current element is the same as the previous element, skip it to avoid duplicates. |
| 53 | +6: Use two pointers, left = j+1 and right = n-1, to find the other two elements in the quadruplet whose sum equals the target value. |
| 54 | +7: If the sum of the four elements is less than the target value, increment left pointer. |
| 55 | +8: If the sum of the four elements is greater than the target value, decrement right pointer. |
| 56 | +9: If the sum of the four elements is equal to the target value, add the quadruplet to the result and increment left and decrement right pointers. |
| 57 | +10: Skip duplicate values of left and right pointers to avoid duplicate quadruplets. |
| 58 | +11: Return the result. |
| 59 | + |
| 60 | + |
| 61 | +### Solution Code |
| 62 | + |
| 63 | +#### Python |
| 64 | + |
| 65 | +```python |
| 66 | +class Solution(object): |
| 67 | + def fourSum(self, nums, target): |
| 68 | + quadruplets = [] |
| 69 | + n = len(nums) |
| 70 | + # Sorting the array |
| 71 | + nums.sort() |
| 72 | + for i in range(n - 3): |
| 73 | + # Skip duplicates |
| 74 | + if i > 0 and nums[i] == nums[i - 1]: |
| 75 | + continue |
| 76 | + for j in range(i + 1, n - 2): |
| 77 | + # Skip duplicates |
| 78 | + if j > i + 1 and nums[j] == nums[j - 1]: |
| 79 | + continue |
| 80 | + left = j + 1 |
| 81 | + right = n - 1 |
| 82 | + while left < right: |
| 83 | + sum = nums[i] + nums[j] + nums[left] + nums[right] |
| 84 | + if sum < target: |
| 85 | + left += 1 |
| 86 | + elif sum > target: |
| 87 | + right -= 1 |
| 88 | + else: |
| 89 | + quadruplets.append([nums[i], nums[j], nums[left], nums[right]]) |
| 90 | + # Skip duplicates |
| 91 | + while left < right and nums[left] == nums[left + 1]: |
| 92 | + left += 1 |
| 93 | + while left < right and nums[right] == nums[right - 1]: |
| 94 | + right -= 1 |
| 95 | + left += 1 |
| 96 | + right -= 1 |
| 97 | + return quadruplets |
| 98 | +``` |
| 99 | + |
| 100 | +#### C++ |
| 101 | + |
| 102 | +```cpp |
| 103 | +class Solution { |
| 104 | +public: |
| 105 | + vector<vector<int>> fourSum(vector<int>& nums, int target) { |
| 106 | + vector<vector<int>> quadruplets; |
| 107 | + int n = nums.size(); |
| 108 | + // Sorting the array |
| 109 | + sort(nums.begin(), nums.end()); |
| 110 | + for (int i = 0; i < n - 3; i++) { |
| 111 | + // Skip duplicates |
| 112 | + if (i > 0 && nums[i] == nums[i - 1]){ |
| 113 | + continue; |
| 114 | + } |
| 115 | + for (int j = i + 1; j < n - 2; j++) { |
| 116 | + // Skip duplicates |
| 117 | + if (j > i + 1 && nums[j] == nums[j - 1]){ |
| 118 | + continue; |
| 119 | + } |
| 120 | + int left = j + 1; |
| 121 | + int right = n - 1; |
| 122 | + while (left < right) { |
| 123 | + long long sum = static_cast<long long>(nums[i]) + nums[j] + nums[left] + nums[right]; |
| 124 | + if (sum < target) { |
| 125 | + left++; |
| 126 | + } else if (sum > target) { |
| 127 | + right--; |
| 128 | + } else { |
| 129 | + quadruplets.push_back({nums[i], nums[j], nums[left], nums[right]}); |
| 130 | + // Skip duplicates |
| 131 | + while (left < right && nums[left] == nums[left + 1]){ |
| 132 | + left++; |
| 133 | + } |
| 134 | + while (left < right && nums[right] == nums[right - 1]){ |
| 135 | + right--; |
| 136 | + } |
| 137 | + left++; |
| 138 | + right--; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + return quadruplets; |
| 144 | + } |
| 145 | +}; |
| 146 | +``` |
| 147 | +
|
| 148 | +#### Java |
| 149 | +
|
| 150 | +```java |
| 151 | +class Solution { |
| 152 | + public List<List<Integer>> fourSum(int[] nums, int target) { |
| 153 | + List<List<Integer>> quadruplets = new ArrayList<>(); |
| 154 | + int n = nums.length; |
| 155 | + // Sorting the array |
| 156 | + Arrays.sort(nums); |
| 157 | + for (int i = 0; i < n - 3; i++) { |
| 158 | + // Skip duplicates |
| 159 | + if (i > 0 && nums[i] == nums[i - 1]) { |
| 160 | + continue; |
| 161 | + } |
| 162 | + for (int j = i + 1; j < n - 2; j++) { |
| 163 | + // Skip duplicates |
| 164 | + if (j > i + 1 && nums[j] == nums[j - 1]) { |
| 165 | + continue; |
| 166 | + } |
| 167 | + int left = j + 1; |
| 168 | + int right = n - 1; |
| 169 | + while (left < right) { |
| 170 | + long sum = (long) nums[i] + nums[j] + nums[left] + nums[right]; |
| 171 | + if (sum < target) { |
| 172 | + left++; |
| 173 | + } else if (sum > target) { |
| 174 | + right--; |
| 175 | + } else { |
| 176 | + quadruplets.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right])); |
| 177 | + // Skip duplicates |
| 178 | + while (left < right && nums[left] == nums[left + 1]) { |
| 179 | + left++; |
| 180 | + } |
| 181 | + while (left < right && nums[right] == nums[right - 1]) { |
| 182 | + right--; |
| 183 | + } |
| 184 | + left++; |
| 185 | + right--; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + return quadruplets; |
| 191 | + } |
| 192 | +} |
| 193 | +``` |
| 194 | +#### Javascript |
| 195 | +``` |
| 196 | +var fourSum = function(nums, target) { |
| 197 | + nums.sort((a, b) => a - b); |
| 198 | + const quadruplets = []; |
| 199 | + const n = nums.length; |
| 200 | + for (let i = 0; i < n - 3; i++) { |
| 201 | + if (i > 0 && nums[i] === nums[i - 1]) { |
| 202 | + continue; |
| 203 | + } |
| 204 | + for (let j = i + 1; j < n - 2; j++) { |
| 205 | + if (j > i + 1 && nums[j] === nums[j - 1]) { |
| 206 | + continue; |
| 207 | + } |
| 208 | + let left = j + 1; |
| 209 | + let right = n - 1; |
| 210 | + while (left < right) { |
| 211 | + const sum = BigInt(nums[i]) + BigInt(nums[j]) + BigInt(nums[left]) + BigInt(nums[right]); |
| 212 | + if (sum < target) { |
| 213 | + left++; |
| 214 | + } else if (sum > target) { |
| 215 | + right--; |
| 216 | + } else { |
| 217 | + quadruplets.push([nums[i], nums[j], nums[left], nums[right]]); |
| 218 | + while (left < right && nums[left] === nums[left + 1]) { |
| 219 | + left++; |
| 220 | + } |
| 221 | + while (left < right && nums[right] === nums[right - 1]) { |
| 222 | + right--; |
| 223 | + } |
| 224 | + left++; |
| 225 | + right--; |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + return quadruplets; |
| 231 | +}; |
| 232 | +``` |
| 233 | +### Conclusion |
| 234 | + |
| 235 | +The given solution sorts the input list and uses a nested loop structure with two pointers to find all unique quadruplets that sum up to the target. By using a set to store the quadruplets, it ensures that duplicates are avoided. The solution efficiently narrows down potential combinations by adjusting the pointers based on the current sum relative to the target. This approach is effective for generating the required output while maintaining uniqueness. |
0 commit comments