forked from Krushna-Prasad-Sahoo/JavaCode-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirstMissingPostive.java
More file actions
34 lines (31 loc) · 873 Bytes
/
firstMissingPostive.java
File metadata and controls
34 lines (31 loc) · 873 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
package com.company;
public class firstMissingPostive {
public static void main(String[] args) {
int[] a ={3,4,-1,1};
System.out.println(first_missing_positive(a));
}
static int first_missing_positive(int[]a){
int i = 0;
while (i < a.length) {
int correct = a[i]-1 ;
if (a[i]>0 && a[i]<=a.length && a[i] != a[correct]) {
swap(a, i, correct);
} else {
i++;
}
}
//serach for first missing number
for (int index = 0; index < a.length; index++) {
if (a[index]!=index+1){
return index+1;
}
}
return a.length+1;
}
static void swap(int[] arr,int i,int correct){
int temp;
temp=arr[i];
arr[i]=arr[correct];
arr[correct]=temp;
}
}