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