JAVA Need OUTPUT Design an algorithm for the 3SUM problem t
JAVA : Need OUTPUT
Design an algorithm for the 3-SUM problem that takes time proportional to n^2 in the worst case. You may assume that you can sort the n integers in time proportional to n^2 or better.Solution
Program:
import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
public class Sorting {
   public static List<List<Integer>> threeSum(int[] nums) {
    List<List<Integer>> result = new ArrayList<List<Integer>>();
      
    if(nums == null || nums.length<3)
    return result;
      
    Arrays.sort(nums);
      
    for(int i=0; i<nums.length-2; i++){
    if(i==0 || nums[i] > nums[i-1]){
    int j=i+1;
    int k=nums.length-1;
      
    while(j<k){
    if(nums[i]+nums[j]+nums[k]==0){
    List<Integer> l = new ArrayList<Integer>();
    l.add(nums[i]);
    l.add(nums[j]);
    l.add(nums[k]);
    result.add(l);
      
    j++;
    k--;
      
    //handle duplicate here
    while(j<k && nums[j]==nums[j-1])
    j++;
    while(j<k && nums[k]==nums[k+1])
    k--;
      
    }else if(nums[i]+nums[j]+nums[k]<0){
    j++;
    }else{
    k--;
    }
    }
    }
      
    }
      
    return result;
    }
    public static void main(String[] args) {
        int numbers[]={-1, 0, 1, 2, -1, -4};
        System.out.println(\"All Combinations Are:\"+threeSum(numbers));
    }
 }
Output:
All Combinations Are:[[-1, -1, 2], [-1, 0, 1]]


