forked from Teju-1212/hacktoberfest-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindmissingnumber.java
More file actions
38 lines (31 loc) · 920 Bytes
/
findmissingnumber.java
File metadata and controls
38 lines (31 loc) · 920 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
public class findmissingnumber {
public static int findmissingnum(int[] arr) {
int number = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != number) {
return number;
}
number++;
}
return -1;
}
// modified binary search
// Time complexity:O(N) and auxillary space: O(1)
public static int findmissingelement(int[] arr, int l, int r) {
int mid = l + (r - l) / 2;
if (l < r) {
return r + 1;
}
if (arr[l] != l) {
return l;
}
if (arr[mid] == mid) {
findmissingelement(arr, mid + 1, r);
}
return findmissingelement(arr, l, mid);
}
public static void main(String[] args) {
int arr[] = { 0, 1, 3, 4, 5, 6, 7, 10 };
System.out.println(findmissingelement(arr, 0, arr.length - 1));
}
}