|
| 1 | +import java.util.*; |
| 2 | +public class ThreeSumProblem { |
| 3 | + public static void main(String args[]) |
| 4 | + { |
| 5 | + Scanner scan = new Scanner(System.in); |
| 6 | + System.out.print("Enter the target sum "); |
| 7 | + int ts= scan.nextInt(); |
| 8 | + System.out.print("Enter the number of elements in the array "); |
| 9 | + int n = scan.nextInt(); |
| 10 | + System.out.println("Enter all your array elements:"); |
| 11 | + int arr[]= new int[n]; |
| 12 | + for(int i=0;i<n;i++) |
| 13 | + { |
| 14 | + arr[i]=scan.nextInt(); |
| 15 | + } |
| 16 | + ThreeSumProblem th = new ThreeSumProblem(); |
| 17 | + System.out.println("Brute Force Approach\n"+(th.BruteForce(arr,ts))+"\n"); |
| 18 | + System.out.println("Two Pointer Approach\n"+(th.TwoPointer(arr,ts))+"\n"); |
| 19 | + System.out.println("Hashmap Approach\n"+(th.Hashmap(arr,ts))); |
| 20 | + |
| 21 | +} |
| 22 | + public List<List<Integer>> BruteForce(int[] nums,int target) { |
| 23 | + List<List<Integer>> arr = new ArrayList<List<Integer>>(); |
| 24 | + |
| 25 | + for(int i=0;i<nums.length;i++) |
| 26 | + { |
| 27 | + for(int j=i+1;j<nums.length;j++) |
| 28 | + { |
| 29 | + for(int k=j+1;k<nums.length;k++) |
| 30 | + { |
| 31 | + if(nums[i]+nums[j]+nums[k]==target) |
| 32 | + { List<Integer> temp = new ArrayList<>(); |
| 33 | + temp.add(nums[i]); |
| 34 | + temp.add(nums[j]); |
| 35 | + temp.add(nums[k]); |
| 36 | + Collections.sort(temp); |
| 37 | + arr.add(temp); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + arr = new ArrayList<List<Integer>>(new LinkedHashSet<List<Integer>>(arr)); |
| 45 | + return arr; |
| 46 | + } |
| 47 | + public List<List<Integer>> TwoPointer(int[] nums, int target) { |
| 48 | + Arrays.sort(nums); |
| 49 | + List<List<Integer>> arr = new ArrayList<List<Integer>>(); |
| 50 | + int start=0; |
| 51 | + int end=0; |
| 52 | + int i = 0; |
| 53 | + while(i<nums.length-1) |
| 54 | + { |
| 55 | + start=i+1; |
| 56 | + end=nums.length-1; |
| 57 | + while (start<end) |
| 58 | + { |
| 59 | + if (nums[start]+nums[end]+nums[i]==target) |
| 60 | + { |
| 61 | + List<Integer> temp = new ArrayList<>(); |
| 62 | + temp.add(nums[i]); |
| 63 | + temp.add(nums[start]); |
| 64 | + temp.add(nums[end]); |
| 65 | + arr.add(temp); |
| 66 | + start++; |
| 67 | + end--; |
| 68 | + } |
| 69 | + else if (nums[start]+nums[end]+nums[i]<target) |
| 70 | + start+=1; |
| 71 | + else |
| 72 | + end-=1; |
| 73 | + |
| 74 | + } |
| 75 | + i++; |
| 76 | + } |
| 77 | + Set<List<Integer>> set = new LinkedHashSet<List<Integer>>(arr); |
| 78 | + return new ArrayList<List<Integer>>(set); |
| 79 | + } |
| 80 | + public List<List<Integer>> Hashmap(int[] nums, int target) { |
| 81 | + Arrays.sort(nums); |
| 82 | + Set<List<Integer>> ts = new HashSet(); |
| 83 | + HashMap<Integer,Integer> hm = new HashMap<>(); |
| 84 | + |
| 85 | + for(int i=0;i<nums.length;i++) |
| 86 | + { |
| 87 | + hm.put(nums[i],i); |
| 88 | + } |
| 89 | + |
| 90 | + for(int i=0;i<nums.length;i++) |
| 91 | + { |
| 92 | + for(int j=i+1;j<nums.length;j++) |
| 93 | + { |
| 94 | + int t= target - nums[i] - nums[j]; |
| 95 | + if(hm.containsKey(t) && hm.get(t)>j) |
| 96 | + { |
| 97 | + List<Integer> temp = new ArrayList<>(); |
| 98 | + temp.add(nums[i]); |
| 99 | + temp.add(nums[j]); |
| 100 | + temp.add(t); |
| 101 | + ts.add(temp); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + return new ArrayList(ts); |
| 106 | + } |
| 107 | + |
| 108 | +} |
| 109 | + |
0 commit comments