-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFirstMissingPositive.java
35 lines (32 loc) · 1.04 KB
/
FirstMissingPositive.java
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
/*https://leetcode.com/problems/first-missing-positive/*/
/*Prateekshya*/
/*Modifying the existing array*/
class Solution {
public int firstMissingPositive(int[] nums) {
int n = nums.length;
boolean isOnePresent = false, isLengthPresent = false;
for (int i = 0; i < n; ++i)
{
if (nums[i] == 1)
isOnePresent = true;
else if (nums[i] == n)
isLengthPresent = true;
if (nums[i] < 0) nums[i] = 0;
}
if (!isOnePresent) return 1;
if (n == 1) return 2;
nums[1] = nums[1] == 0 ? -1 : nums[1]*(-1);
for (int i = 0; i < n; ++i)
{
int absValue = Math.abs(nums[i]);
if (absValue < nums.length && absValue != 1 && nums[absValue] >= 0)
{
nums[absValue] = nums[absValue] == 0 ? -1 : nums[absValue]*(-1);
}
}
for (int i = 1; i < n; ++i)
if (nums[i] >= 0)
return i;
return isLengthPresent ? n+1 : n;
}
}