-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathsolution.java
More file actions
30 lines (25 loc) · 835 Bytes
/
Copy pathsolution.java
File metadata and controls
30 lines (25 loc) · 835 Bytes
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
class Solution {
public List<Integer> findMissingElements(int[] nums) {
// Find the smallest and largest values
int mn = Integer.MAX_VALUE;
int mx = Integer.MIN_VALUE;
// Store all numbers for fast lookup
HashSet<Integer> seen = new HashSet<>();
for (int num : nums) {
mn = Math.min(mn, num);
mx = Math.max(mx, num);
seen.add(num);
}
// Store missing numbers
List<Integer> ans = new ArrayList<>();
// Check every number in the range
for (int num = mn; num <= mx; num++) {
// If the number does not exist, add it
if (!seen.contains(num)) {
ans.add(num);
}
}
// Return the result
return ans;
}
}