-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissingNumber.java
More file actions
39 lines (32 loc) · 888 Bytes
/
missingNumber.java
File metadata and controls
39 lines (32 loc) · 888 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
package com.company;
public class missingNumber {
public static void main(String[] args) {
int[] arr = {4, 0, 5, 1, 3};
System.out.println(sort(arr));
}
/* there will be two cases
second when the N number is missing
*/
static int sort(int[] nums) {
int i = 0;
while (i < nums.length) {
if (nums[i] < nums.length && i != nums[i]) {
swap(nums, i, nums[i]);
} else {
i++;
}
}
//search
for (int index = 0; index < nums.length; index++) {
if (nums[index] != index) {
return index;
}
}
return nums.length;
}
static void swap(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}