(int[] n, int target)
| 26 | } |
| 27 | } |
| 28 | public static int[] twoSum(int[] n, int target) { |
| 29 | Map<Integer,Integer> nm=new HashMap<>(); |
| 30 | // hashmap is created to store the array elements |
| 31 | int array[]=new int[2]; |
| 32 | int k=n.length; |
| 33 | for(int i=0;i<k;i++) |
| 34 | { |
| 35 | if(nm.containsKey(target-n[i])) |
| 36 | { |
| 37 | array[0]=nm.get(target-n[i]); |
| 38 | array[1]=i; |
| 39 | break; |
| 40 | // The sum of the elements equals to the target is storee |
| 41 | } |
| 42 | nm.put(n[i],i); |
| 43 | } |
| 44 | return array; |
| 45 | // the answer array is returned |
| 46 | } |
| 47 | } |