-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissingGnomes.java
More file actions
46 lines (40 loc) · 1.3 KB
/
Copy pathMissingGnomes.java
File metadata and controls
46 lines (40 loc) · 1.3 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
import java.util.Scanner;
import java.util.TreeSet;
public class MissingGnomes {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
int n = Integer.parseInt(line.substring(0, line.indexOf(" ")));
int m = Integer.parseInt(line.substring(line.indexOf(" ")+1));
TreeSet<Integer> set = new TreeSet<>();
int[] gnomes = new int[m];
// add all the gnomes
for (int i = 1; i <= n; i++) {
set.add(i);
}
int j = 0;
for (int i = 0; i < m; i++) {
int temp = sc.nextInt();
if(set.contains(temp)) set.remove(temp);
gnomes[j++] = temp;
}
gnomeOrder(set, gnomes);
}
public static void gnomeOrder(TreeSet<Integer> set, int[] gnomes) {
int i = 0; //incrementer of the array
while(i < gnomes.length && set.size() != 0){
if(gnomes[i] > set.first()){
System.out.println(set.pollFirst());
}
else{
System.out.println(gnomes[i++]);
}
}
while(i < gnomes.length){
System.out.println(gnomes[i++]);
}
while(set.size() != 0){
System.out.println(set.pollFirst());
}
}
}