I am working on a problem 3Sum, in which, given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0. I must find all unique triplets in the array which gives the sum of zero.I wrote my naive implementation which was \$O(n^3)\$ since it was checking for every possible combination:public List<List<Integer>> threeSum(int[] num) {    List<List<Integer>> res=new ArrayList<List<Integer>>();        if(num.length<3) return res;       Arrays.sort(num);        for(int i=0;i<num.length;i++)        {         if(num[i]>0) break;            for(int j=i+1;j<num.length;j++)            {                if(num[i]+num[j]>0 && num[j]>0) break;                for(int k=j+1;k<num.length;k++)                {                    if(num[i]+num[j]+num[k]==0)                      {                          List<Integer> curr=new ArrayList<Integer>();                          curr.add(num[i]);                          curr.add(num[j]);                          curr.add(num[k]);                          res.add(curr);                      }                      while(k+1<num.length && num[k]==num[k+1]) k++;                }                 while(j+1<num.length && num[j]==num[j+1]) j++;            }            while(i+1<num.length && num[i]==num[i+1]) i++;        }        return res;}However, I am looking for a solution which is better than \$O(n^2)\$. Rather, is there a way I can bring the time complexity of my solution to \$O(n^2)\$ by removing any of the loops?
Finding unique triplets adding up to 0
java;programming challenge;complexity