forked from VaibhavD74/HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmissingNumber.java
More file actions
36 lines (31 loc) · 893 Bytes
/
missingNumber.java
File metadata and controls
36 lines (31 loc) · 893 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
package com.saikat;
import java.util.Arrays;
public class missingNumber {
public static void main(String[] args) {
int[] arr = {4,2,1,0};
miss(arr);
System.out.println(miss(arr));
}
static int miss(int[] arr){
int i = 0;
while (i < arr.length){
int correctIndex = arr[i];
if (arr[i] < arr.length && arr[i] != arr[correctIndex]){
swap(arr,i,correctIndex);
}else{
i++;
}
}
for (int index = 0; index < arr.length; index++) {
if (arr[index] != index){
return index;
}
}
return arr.length;
}
static void swap(int[] arr, int first, int second){
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
}