- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindMissing.java
More file actions
Latest commit
45 lines (36 loc) · 944 Bytes
/
Copy pathFindMissing.java
File metadata and controls
45 lines (36 loc) · 944 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
publicclassFindMissing {
publicstaticvoidmain(String[] args) {
intarr[] = { 1, 2, 4, 6, 3, 7, 5 };
intn = 8;
intmis = find(arr, n);
intmis1 = find1(arr, n);
System.out.println(mis);
System.out.println(mis1);
}
staticintfind(int[] arr, intn) {
intsum1 = (n * (n + 1)) / 2;
intsum = 0;
intmis = 0;
for (inti = 0; i < n - 1; i++) {
sum += arr[i];
}
mis = sum1 - sum;
returnmis;
}
staticintfind1(int[] arr, intn) {
inttemp[] = newint[n + 1];
intans = 0;
for (inti = 0; i < n; i++) {
temp[i] = 0;
}
for (inti = 0; i < n - 1; i++) {
temp[arr[i] - 1] = 1;
}
for (inti = 0; i < n; i++) {
if (temp[i] == 0) {
ans = i + 1;
}
}
returnans;
}
}