-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirstMissingPos.java
More file actions
executable file
·55 lines (46 loc) · 1.29 KB
/
Copy pathfirstMissingPos.java
File metadata and controls
executable file
·55 lines (46 loc) · 1.29 KB
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
45
46
47
48
49
50
51
52
53
54
55
// Java program for the above approach
import java.util.*;
public class firstMissingPos{
// Function for finding the first
// missing positive number
static int finding(int arr[], int n)
{
System.out.println("Initial Array\n");
System.out.println(Arrays.toString(arr));
// Loop to traverse the whole array
for (int i = 0; i < n; i++) {
// Loop to check boundary
// condition and for swapping
while (arr[i] >= 1 && arr[i] <= n
&& arr[i] != arr[arr[i] - 1]) {
swap(arr,i, arr[i] - 1);
System.out.println(Arrays.toString(arr));
}
}
System.out.println("Final Array\n");
System.out.println(Arrays.toString(arr));
// Checking any element which
// is not equal to i+1
for (int i = 0; i < n; i++) {
if (arr[i]!= i + 1) {
return i + 1;
}
}
// Nothing is present return last index
return n + 1;
}
static void swap(int arr[],int a,int b){
int temp = arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
// Driver Code
public static void main(String[] args){
// int arr[] = {4,2,3, 3, -7, 6, 8, 1, -10, 15};
// int arr[] = {4,2,7,1,5,6,3};
int arr[] = {1,2,3,4,5,6,7,20};
int n = arr.length;
int ans = finding(arr, n);
System.out.println("Result: " +ans);
}
}