diff --git a/Misc/ThreeSumProblem.java b/Misc/ThreeSumProblem.java new file mode 100644 index 000000000..a5ccae862 --- /dev/null +++ b/Misc/ThreeSumProblem.java @@ -0,0 +1,109 @@ +import java.util.*; +public class ThreeSumProblem { + public static void main(String args[]) + { + Scanner scan = new Scanner(System.in); + System.out.print("Enter the target sum "); + int ts= scan.nextInt(); + System.out.print("Enter the number of elements in the array "); + int n = scan.nextInt(); + System.out.println("Enter all your array elements:"); + int arr[]= new int[n]; + for(int i=0;i> BruteForce(int[] nums,int target) { + List> arr = new ArrayList>(); + + for(int i=0;i temp = new ArrayList<>(); + temp.add(nums[i]); + temp.add(nums[j]); + temp.add(nums[k]); + Collections.sort(temp); + arr.add(temp); + } + + + } + } + } + arr = new ArrayList>(new LinkedHashSet>(arr)); + return arr; + } + public List> TwoPointer(int[] nums, int target) { + Arrays.sort(nums); + List> arr = new ArrayList>(); + int start=0; + int end=0; + int i = 0; + while(i temp = new ArrayList<>(); + temp.add(nums[i]); + temp.add(nums[start]); + temp.add(nums[end]); + arr.add(temp); + start++; + end--; + } + else if (nums[start]+nums[end]+nums[i]> set = new LinkedHashSet>(arr); + return new ArrayList>(set); + } + public List> Hashmap(int[] nums, int target) { + Arrays.sort(nums); + Set> ts = new HashSet(); + HashMap hm = new HashMap<>(); + + for(int i=0;ij) + { + List temp = new ArrayList<>(); + temp.add(nums[i]); + temp.add(nums[j]); + temp.add(t); + ts.add(temp); + } + } + } + return new ArrayList(ts); + } + +} + diff --git a/Misc/TwoSumProblem.java b/Misc/TwoSumProblem.java index 0bea06f39..620f5d883 100644 --- a/Misc/TwoSumProblem.java +++ b/Misc/TwoSumProblem.java @@ -1,5 +1,4 @@ package Misc; - import java.util.*; import java.util.stream.Collectors;