-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
131 lines (96 loc) · 4.31 KB
/
Copy pathTwoSum.java
File metadata and controls
131 lines (96 loc) · 4.31 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package gliderai;
import java.util.HashMap;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class TwoSum {
public int[] twoSum(int[] nums, int target) {
// Java 17 feature: var for Type Inference
if (nums == null || nums.length < 2) {
throw new IllegalArgumentException("Input array must have at least two elements.");
}
var map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("No solution found: No two numbers sum up to the target.");
}
//Before Java 17
public int[] twoSumSol(int[] nums, int target) {
//Before Java 17
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
// Check if complement exists in the map
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
// Store the current number with its index
map.put(nums[i], i);
}
return new int[]{}; // Should never reach here as per problem constraints
}
// find all valid pairs in the array
public int[] twoSumStreamSol(int[] nums, int target) {
// find all valid pairs in the array
var map1 = new HashMap<Integer, Integer>();
// add return to Line 61
IntStream.range(0, nums.length)
.mapToObj(i -> {
int complement = target - nums[i];
if (map1.containsKey(complement)) {
return new int[]{map1.get(complement), i};
}
map1.put(nums[i], i);
return null;
})
.filter(Objects::nonNull) // Remove null values
.collect(Collectors.toList()); // Collect all valid pairs
// Java 8 and later --> Find only one pair in the array
if (nums == null || nums.length < 2) {
throw new IllegalArgumentException("Input array must have at least two elements.");
}
var map = new HashMap<Integer, Integer>();
return IntStream.range(0, nums.length)
.mapToObj(i -> {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[]{map.get(complement), i};
}
map.put(nums[i], i);
return null;
})
.filter(result -> result != null) // Filter out null results
.findFirst() // Get the first valid result
.orElseThrow(() -> new IllegalArgumentException("No solution found: No two numbers sum up to the target."));
}
public static void main(String[] args) {
TwoSum solution = new TwoSum();
try {
int[] nums = { 2, 7, 11, 15 };
int target = 9;
int[] result = solution.twoSum(nums, target);
System.out.println("Indices: [" + result[0] + ", " + result[1] + "]");
} catch (IllegalArgumentException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
/* New Features Used from Java 17
1. var for Type Inference – Reduces verbosity (used for HashMap).
2. Enhanced Exception Handling – Uses IllegalArgumentException with meaningful messages.
3. Improved Readability & Robustness.
Edge Case How It’s Handled
Null Array (null) Throws IllegalArgumentException
Array with Less than 2 Elements ([1]) Throws IllegalArgumentException
No Valid Pair Exists ([1, 2, 3], target=10) Throws IllegalArgumentException
Negative Numbers ([-1, -2, -3, -4]) Works correctly
Zeros and Duplicates ([0, 0, 3, 4], target=0) Works correctly
Large Input Size (10^6 elements) Runs in O(n) and performs well
*/
// Time Complexity: O(n)
// Space Complexity: O(n)