-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_Missing_Elements.java
More file actions
70 lines (44 loc) · 1.61 KB
/
Copy pathFind_Missing_Elements.java
File metadata and controls
70 lines (44 loc) · 1.61 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
/*
You are given an integer array nums consisting of unique integers.
Originally, nums contained every integer within a certain range. However, some integers might have gone missing from the array.
The smallest and largest integers of the original range are still present in nums.
Return a sorted list of all the missing integers in this range. If no integers are missing, return an empty list.
Example 1:
Input: nums = [1,4,2,5]
Output: [3]
Explanation:
The smallest integer is 1 and the largest is 5, so the full range should be [1,2,3,4,5]. Among these, only 3 is missing.
Example 2:
Input: nums = [7,8,6,9]
Output: []
Explanation:
The smallest integer is 6 and the largest is 9, so the full range is [6,7,8,9]. All integers are already present, so no integer is missing.
Example 3:
Input: nums = [5,1]
Output: [2,3,4]
Explanation:
The smallest integer is 1 and the largest is 5, so the full range should be [1,2,3,4,5]. The missing integers are 2, 3, and 4.
Constraints:
2 <= nums.length <= 100
1 <= nums[i] <= 100
*/
import java.util.*;
class Find_Missing_Elements {
public List<Integer> findMissingElements(int[] nums) {
Arrays.sort(nums);
int n = nums.length ;
ArrayList<Integer> ans = new ArrayList<>();
HashMap <Integer,Integer> map = new HashMap<>();
for(int i = 0 ; i<n; i++){
map.put(nums[i], map.getOrDefault(nums[i],0)+1);
}
int max = nums[n-1];
int min = nums[0];
for(int i = min ; i<max ; i++){
if(!map.containsKey(i)){
ans.add(i) ;
}
}
return ans;
}
}