-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstMissingPositive.java
More file actions
82 lines (66 loc) · 2.44 KB
/
FirstMissingPositive.java
File metadata and controls
82 lines (66 loc) · 2.44 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
https://leetcode.com/problems/first-missing-positive/
Given an unsorted integer array nums, return the smallest missing positive integer.
You must implement an algorithm that runs in O(n) time and uses constant extra space.
Example 1:
Input: nums = [1,2,0]
Output: 3
Example 2:
Input: nums = [3,4,-1,1]
Output: 2
Example 3:
Input: nums = [7,8,9,11,12]
Output: 1
Constraints:
1 <= nums.length <= 5 * 105
-231 <= nums[i] <= 231 - 1
*/
public class FirstMissingPositive {
public static void main(String[] args) {
//int[] nums = { 3, 4, -1, 1 };
int[] nums = {4,2,1,-1,-4,0 };
//int[] nums = {3,4,5,6};
// int[] nums = {1,1};
//int[] nums = {1,2,3};
System.out.println(missingPositive(nums));
}
public static int missingPositive(int[] nums) {
// example [4,2,1,-1,-4,0]
int n = nums.length;
// based on the length missing number must be in the set
// {1,2,3,...,n}
int i = 0; // index for the array
while (i < n) {
if (nums[i] >= 1 && nums[i] <= n) {// if the number is in the set
int pos = nums[i] - 1; // position corresponding to nums[i]
// IF number not in correct position AND number is different than
// number in swap position
if (pos != i && nums[i] != nums[pos]) {
// swap nums[i] and nums[pos]
int temp = nums[pos];
nums[pos] = nums[i];
nums[i] = temp;
} else // if number is in correct position or same as number in swap position
i = i + 1; // go to next number
} else { // if number is not in range {1,2,...n}
i = i + 1; // move to next element
}
}
// example: [1 2 -1 4 -4 0 ]
//printArray(nums);
// find the first position
// that does not have the correct element
for (i = 0; i < n; i++)
if (nums[i] != i + 1)
return i + 1;
//if all positions are in correct spot, eg. [1,2,3...n] then return
// next positive
return n+1; // all numbers in correct positions [1,2,3]
}
// This simple method prints the numbers in the array.
public static void printArray(int[] nums) {
for (int i = 0; i < nums.length; i++)
System.out.print(nums[i] + " ");
System.out.println();
}
}