-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMissingNo.java
44 lines (37 loc) · 1.48 KB
/
MissingNo.java
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
//return the only number in range that are missing from array;
//[3,0,1]-->2 is missing that we have find
// when numberrs are start from the 0 then we have to take index i.e there are n+1 nubers present in array ;
// and when numbers are start fromm the 1 then wee have to take the index-1 i.e means there are n numbers are present in the arrray;
public class MissingNo {
public static void main(String[] args) {
// Example array: [0, 3, 1] --> 2 is missing
int arr[] = {0, 3, 1};
int missing = findMissingNo(arr);
System.out.println("Missing number is: " + missing);
}
public static int findMissingNo(int arr[]) {
int i = 0;
while (i < arr.length) {
int correct = arr[i];
// Ensure correct is within bounds
if (arr[i] < arr.length && arr[i] != arr[correct]) {
swap(arr, i, correct);
} else {
i++;
}
}
// Check for the missing number
for (int index = 0; index < arr.length; index++) {
if (arr[index] != index) {
return index;
}
}
// If all numbers are in place, the missing number is the last one
return arr.length;
}
public static void swap(int arr[], int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}