I am trying to write a code to find a triplet whose sum is equal to given target, using list of lists but it is throwing error. I tried increasing heap size in IntelliJ as well as Eclipse, but it still throws error. Following is my code:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class TripleSum {
public List<List<Integer>> findNumbers(int[] nums, int target){
Arrays.sort(nums);
List<List<Integer>> al = new ArrayList<List<Integer>>();
ArrayList<Integer> lst = new ArrayList<>();
int l, r;
int n = nums.length;
for(int i=0; i<n; i++){
l=i+1;
r= n-1;
while(l<r){
if(nums[i]+nums[l]+nums[r]==target){
lst.add(nums[i]);
//lst.add(l);
//lst.add(r);
}
else if(nums[i]+nums[l]+nums[r]<target)
l++;
else
r--;
}
al.add(lst);
}
return al;
}
public static void main(String[] args){
int[] arr = {12, 3, 4, 1, 6, 9, 6};
TripleSum ts = new TripleSum();
System.out.println(ts.findNumbers(arr, 24));
}
}