- 4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
The solution set must not contain duplicate quadruplets.
Example
Solution 1
Time Complexity: O(n^3), Space Complexity: O(1)
Ignored Case
- the numbers can be negative
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(nums);
for(int i = 0; i < nums.length-3; i++) {
if(i > 0 && nums[i-1] == nums[i])
continue;
int rem = target-nums[i];
for(int j = i+1; j < nums.length-2; j++) {
if(j > i+1 && nums[j-1] == nums[j])
continue;
int start = j+1;
int end = nums.length-1;
while(start < end) {
int sum = nums[j]+nums[start]+nums[end];
if(sum == rem) {
List<Integer> quaruplet = new ArrayList<Integer>();
quaruplet.add(nums[i]);
quaruplet.add(nums[j]);
quaruplet.add(nums[start]);
quaruplet.add(nums[end]);
res.add(quaruplet);
start++;
end--;
while(start < end && nums[start-1] == nums[start])
start++;
while(start < end && nums[end+1] == nums[end])
end--;
} else if(sum < rem) {
start++;
} else {
end--;
}
}
}
}
return res;
}
}
Solution 2: recursive, suitable for any X sum requirements
Time Complexity: O(n^4), Space Complexity: O(1)
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
Arrays.sort(nums);
dfs(res, new ArrayList<Integer>(), nums, 0, target);
return res;
}
private void dfs(List<List<Integer>> res, List<Integer> candidates, int[] nums, int curIndex, int target) {
// the size of qualified list
if(candidates.size() == 4) {
if(getSum(candidates) == target) {
res.add(new ArrayList<>(candidates));
}
return;
}
for(int i = curIndex; i < nums.length; i++) {
if(i > curIndex && nums[i-1]==nums[i])
continue;
candidates.add(nums[i]);
if(getSum(candidates) > target && nums[i] > 0) {
if (!candidates.isEmpty()) {
candidates.remove(candidates.size() - 1);
}
return;
}
dfs(res, candidates, nums, i + 1, target);
if (!candidates.isEmpty()) {
candidates.remove(candidates.size() - 1);
}
}
}
private int getSum(List<Integer> candidates) {
int sum = 0;
for(int item: candidates)
sum+=item;
return sum;
}
}
References:
Solution 3: use hash set to store the sum of two int in the array, then check if the addition of two sum equals to the target
Time Complexity: O(n^2), Space Complexity: O(n^2)
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
Set<List<Integer>> listSet = new HashSet<>();
int n = nums.length;
Arrays.sort(nums);
HashMap<Integer, List<Integer[]>> hashMap = new HashMap<>();
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
int num = nums[i] + nums[j];
Integer[] pair = {i, j};
if(hashMap.containsKey(num)) {
hashMap.get(num).add(pair);
}else {
List<Integer[]> list = new ArrayList<>();
list.add(pair);
hashMap.put(num, list);
}
}
}
for(Integer integer : hashMap.keySet()) {
if(hashMap.containsKey(target - integer)) {
List<Integer[]> list1 = hashMap.get(integer);
List<Integer[]> list2 = hashMap.get(target - integer);
for(Integer[] pair1 : list1) {
int index1 = pair1[0];
int index2 = pair1[1];
for(Integer[] pair2 : list2) {
int index3 = pair2[0];
int index4 = pair2[1];
if(index2 < index3) {
List<Integer> list = new ArrayList<>();
list.add(nums[index1]);
list.add(nums[index2]);
list.add(nums[index3]);
list.add(nums[index4]);
listSet.add(list);
}
}
}
}
}
return new ArrayList<>(listSet);
}
}
Reference