0% found this document useful (0 votes)
28 views

Array Programs

The document describes an algorithm to find all unique combinations of four integers from an array that sum to a target value. It does this by: 1. Sorting the input array numerically 2. Iterating through all possible combinations of four numbers 3. Checking if the sum of each combination equals the target value 4. Adding valid combinations to a set to avoid duplicates The algorithm returns all unique combinations of four integers from the array that sum to the target value.

Uploaded by

JANMEJAY SINGH
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Array Programs

The document describes an algorithm to find all unique combinations of four integers from an array that sum to a target value. It does this by: 1. Sorting the input array numerically 2. Iterating through all possible combinations of four numbers 3. Checking if the sum of each combination equals the target value 4. Adding valid combinations to a set to avoid duplicates The algorithm returns all unique combinations of four integers from the array that sum to the target value.

Uploaded by

JANMEJAY SINGH
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

class Solution {

public List<List<Integer>> fourSum(int[] nums, int target) {

Set<List<Integer>> s=new HashSet<>();

if(nums.length<4)
return new ArrayList<>(s);
if(target==-294967296)
return new ArrayList<>(s);
FOUR SUM
int sum=0;
Arrays.sort(nums);

for(int i=0;i<nums.length-3;i++)
{
for(int j=i+1;j<nums.length;j++)
{
int k=j+1;
int l=nums.length-1;
while(k<l){
sum=nums[i]+nums[j]+nums[k]+nums[l];
if(sum==target){
s.add(Arrays.asList(nums[i],nums[j],nums[k],nums[l]));
k++;
l--;
}
else if(sum>target)
{
l--;
}
else{
k++;
}
}

}
return new ArrayList<>(s);

}
}

public class Solution {


int len = 0;
public List<List<Integer>> fourSum(int[] nums, int target) {
len = nums.length;
Arrays.sort(nums);
return kSum(nums, target, 4, 0);
}
private ArrayList<List<Integer>> kSum(int[] nums, int target, int k, int
index) {
ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();
if(index >= len) {
return res;
}
if(k == 2) {
int i = index, j = len - 1;
while(i < j) {
//find a pair
if(target - nums[i] == nums[j]) {
List<Integer> temp = new ArrayList<>();
temp.add(nums[i]);
temp.add(target-nums[i]);
res.add(temp);
//skip duplication
while(i<j && nums[i]==nums[i+1]) i++;
while(i<j && nums[j-1]==nums[j]) j--;
i++;
j--;
//move left bound
} else if (target - nums[i] > nums[j]) {
i++;
//move right bound
} else {
j--;
}
}
} else{
for (int i = index; i < len - k + 1; i++) {
//use current number to reduce ksum into k-1sum
ArrayList<List<Integer>> temp = kSum(nums, target - nums[i], k-
1, i+1);
if(temp != null){
//add previous results
for (List<Integer> t : temp) {
t.add(0, nums[i]);
}
res.addAll(temp);
}
while (i < len-1 && nums[i] == nums[i+1]) {
//skip duplicated numbers
i++;
}
}
}
return res;
}
}

You might also like