forked from saritapatel8770/Leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirstMissingPositive.java
More file actions
33 lines (26 loc) · 823 Bytes
/
firstMissingPositive.java
File metadata and controls
33 lines (26 loc) · 823 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
//importing the required package
import java.util.*;
public class firstMissingPositive {
// creating the function for leetcode
public static int firstMissingPositiveNumber(int[] nums) {
// sorting the given array to arrange them in ascending order.
Arrays.sort(nums);
// declaring the required values
int miss = 1;
int n = nums.length;
// checking for the missing number
for (int i = 0; i < n; i++) {
if (nums[i] > miss)
return miss;
if (nums[i] == miss)
miss++;
}
return miss;
}
// main function
public static void main(String[] args) {
int[] arr1 = { 3, 4, -1, 1 };
int ans = firstMissingPositiveNumber(arr1);
System.out.println(ans);
}
}