-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution2.java
44 lines (35 loc) · 1.09 KB
/
Solution2.java
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
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.HashMap;
import java.util.Scanner;
import java.util.Map;
class Solution2{
// O(n) time | O(n) space
// Hashmap/Dictionary Approach
private static int[] findTwoSum_2(int[] nums, int target) {
Map<Integer, Integer> numMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int diff = target - nums[i];
if (numMap.containsKey(diff)) {
return new int[] { diff, nums[i] };
} else {
numMap.put(nums[i], i);
}
}
return new int[] {};
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
for(int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
int target = sc.nextInt();
sc.close();
int[] pair_arr = findTwoSum_2(nums, target);
if (pair_arr.length == 2) {
System.out.println(pair_arr[0] + " " + pair_arr[1]);
} else {
System.out.println("No pair found!");
}
}
}