https://leetcode.com/problems/3sum/description/ class Solution { public List> threeSum(int[] arr) { int n=arr.length; Set> ans=new HashSet<>(); //Brute force approach for(int i=0;i al=new ArrayList<>(); if(arr[i]+arr[j]+arr[k]==0){ al=Arrays.asList(arr[i],arr[j],arr[k]); Collections.sort(al); ans.add(al); } } } } List> fin=new ArrayList<>(ans); return fin; } } ========================================================== What is time complexity of this Program O(n^3) Will go for Hashing Approach ===================================================== Map 0 1 1 1 2 1 -1 1 -4 1 ==================================================== 0-(-1+0)=1 0-(-1+1)=0 0-(-1+2)= 0-(-1-1)=2 arr= [-1,0,1,2,-1,-4] target-(arr[i]+arr[j]) ================================================== class Solution { public List> threeSum(int[] arr) { int n=arr.length; Set> ans=new HashSet<>(); //Brute force approach for(int i=0;i hs=new HashSet<>(); for(int j=i+1;j ll=Arrays.asList(arr[i],arr[j],complement); Collections.sort(ll); ans.add(ll); } hs.add(arr[j]); } } List> fin=new ArrayList<>(ans); return fin; } } ====================================================== 2 Pointer approach : ======================= Sort the Array you fix the arr[i] j=i+1; k=arr.length-1; while(j0) { k--; } if(sum<0) { j++; } } ========================================= for(int i=0;i hs=new HashSet<>(); for(int j=i+1;j ll=Arrays.asList(arr[i],arr[j],complement); Collections.sort(ll); ans.add(ll); } hs.add(arr[j]); } } ======================================= class Solution { public List> threeSum(int[] arr) { int n=arr.length; Set> ans=new HashSet<>(); //Brute force approach Arrays.sort(arr); for(int i=0;i0){ k--; }else{ j++; } } } List> fin=new ArrayList<>(ans); return fin; } }