MCPcopy Create free account
hub / github.com/PrajaktaSathe/Java / twoSum

Method twoSum

Programs/Two_Sum.java:28–46  ·  view source on GitHub ↗
(int[] n, int target)

Source from the content-addressed store, hash-verified

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}

Callers 1

mainMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected