-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTargetTwoSum.java
More file actions
67 lines (59 loc) · 2.34 KB
/
TargetTwoSum.java
File metadata and controls
67 lines (59 loc) · 2.34 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package assessmentDay;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
public class TargetTwoSum {
// TIME COMPLEXITY: O(N^2); SPACE COMPLEXITY: O(1)
public static int[] twoSum (int[] inputArray, int targetSum) {
int[] result = new int[2];
for (int i = 0; i < inputArray.length; i++) {
for (int j = i + 1; j < inputArray.length; j++) {
if (inputArray[i] + inputArray[j] == targetSum) {
result[0] = inputArray[i];
result[1] = inputArray[j];
}
}
}
return result;
}
public static List<Integer> twoSums (List<Integer> inputArray, int targetSum) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < inputArray.size(); i++) {
for (int j = 0; j < inputArray.size(); j++) {
if (inputArray.get(i) + inputArray.get(j) == targetSum) {
result.add(inputArray.get(i));
result.add(inputArray.get(j));
return result;
}
}
}
return result;
}
// OPTIMIZED SOLUTION WITH TIME COMPLEXITY OF O(N); SPACE COMPLEXITY = O(1)
public static int[] twoSumOptimized(int[] inputArray, int targetSum) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < inputArray.length; i++) {
int complement = targetSum - inputArray[i];
if (map.containsKey(complement)) {
return new int[]{complement, inputArray[i]};
}
map.put(inputArray[i], i);
}
return new int[0];
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 15};
List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 15);
int target = 18;
int[] result = TargetTwoSum.twoSum(arr, target);
List<Integer> resultList = TargetTwoSum.twoSums (integerList, target);
int[] optimizedResult = TargetTwoSum.twoSumOptimized(arr, target);
System.out.println(Arrays.toString(result));
System.out.println(resultList);
System.out.println(Arrays.toString(optimizedResult));
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
}
}