-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
32 lines (28 loc) · 1.1 KB
/
TwoSum.java
File metadata and controls
32 lines (28 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap();
for(int i = 0; i < nums.length; i++ ){
map.put(nums[i], i);
}
for(int i = 0; i < nums.length; i++ ){
int complement = target - nums[i];
if(map.containsKey(complement) && map.get(complement) !=i ){
return new int[]{i, map.get(complement)};
}
}
throw new IllegalArgumentException("No two sum solution");
}
}
/*
Approach: one iteration -> to initialize auxilary map
second iteration -> to find complement
Time complexity: O(n)
Space complexity: O(n)
Logic:
first of all we iterate trough array initialize map with key as number and value as index where number is located;
then with another iteration we are identifying diference between target and
given number nums[i] as complement number
and trying to locate such key in map that has value different than other index;
this logic guarantees us that we will use number from two different indices,
as we cannot use same element twice
*/