Skip to content

Commit 7efba86

Browse files
Arrays: 41. First Missing Positive
1 parent 912d1ab commit 7efba86

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
* [1996](https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/). The Number of Weak Characters in Game - [code](src/arrays/medium/NumberOfWeakCharacters.java)
1919
* [503](https://leetcode.com/problems/next-greater-element-ii/). Next Greater Element II - [code](src/arrays/medium/NextGreaterElementCircular.java)
2020
* [75](https://leetcode.com/problems/sort-colors/). Sort Colors - [code](src/arrays/medium/Sort0s1s2s.java)
21+
* Hard:
22+
* [41](https://leetcode.com/problems/first-missing-positive/). First Missing Positive - [code](src/arrays/hard/FirstMissingPositive.java)
2123

2224
## Math:
2325
* Easy:
@@ -28,6 +30,7 @@
2830
* [231](https://leetcode.com/problems/power-of-two/). Power of Two - [code](src/math/easy/PowerOfTwo.java)
2931
* [1175](https://leetcode.com/problems/prime-arrangements/). Prime Arrangements - [code](src/math/easy/PrimeArrangements.java)
3032
* [258](https://leetcode.com/problems/add-digits/). Add Digits - [code](src/math/easy/AddDigits.java)
33+
* [202](https://leetcode.com/problems/happy-number/). Happy Number - [code](src/math/easy/HappyNumber.java)
3134
* [Gfg](https://practice.geeksforgeeks.org/problems/lcm-and-gcd4516/1). LCM and GCD - [code](src/math/easy/GCDAndLCM.java)
3235
* [Gfg](https://practice.geeksforgeeks.org/problems/armstrong-numbers2727/1). Armstrong Number - [code](src/math/easy/ArmstrongNumberCheck.java)
3336
* [Gfg](https://practice.geeksforgeeks.org/problems/count-digits5716/1). Count digits - [code](src/math/easy/CountDigitsGFG.java)
+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package arrays.hard;
2+
3+
import java.util.Arrays;
4+
5+
/***
6+
* Problem 41 in Leetcode: https://leetcode.com/problems/first-missing-positive/
7+
*
8+
* Given an unsorted integer array nums, return the smallest missing positive integer.
9+
* You must implement an algorithm that runs in O(n) time and uses constant extra space.
10+
*
11+
* Example 1:
12+
* Input: nums = [1,2,0]
13+
* Output: 3
14+
*
15+
* Example 2:
16+
* Input: nums = [3,4,-1,1]
17+
* Output: 2
18+
*
19+
* Example 3:
20+
* Input: nums = [7,8,9,11,12]
21+
* Output: 1
22+
*/
23+
24+
public class FirstMissingPositive {
25+
public static void main(String[] args) {
26+
// int[] nums = {7, 8, 9, 11, 12};
27+
int[] nums = {1, 2, 3, 4, 5};
28+
29+
System.out.println("Brute Force: " + firstMissingPositiveBruteForce(nums));
30+
System.out.println("Sorting: " + firstMissingPositiveSorting(nums));
31+
System.out.println("Count Array: " + firstMissingPositiveCountArray(nums));
32+
System.out.println("Swapping: " + firstMissingPositiveSwapping(nums));
33+
System.out.println("Negation: " + firstMissingPositiveNegation(nums));
34+
}
35+
36+
private static int firstMissingPositiveBruteForce(int[] nums) {
37+
int n = nums.length;
38+
39+
for (int i = 1; i <= n; i++) {
40+
boolean found = false;
41+
for (int num : nums) {
42+
if (num == i) {
43+
found = true;
44+
break;
45+
}
46+
}
47+
if (!found) {
48+
return i;
49+
}
50+
}
51+
52+
return n + 1;
53+
}
54+
55+
private static int firstMissingPositiveSorting(int[] nums) {
56+
Arrays.sort(nums);
57+
58+
int missingNumber = 1;
59+
60+
for (int num : nums) {
61+
if (num == missingNumber) {
62+
missingNumber++;
63+
}
64+
}
65+
66+
return missingNumber;
67+
}
68+
69+
private static int firstMissingPositiveCountArray(int[] nums) {
70+
int n = (int) 1e5 + 2;
71+
int[] count = new int[n];
72+
73+
for (int num : nums) {
74+
if (num > 0) {
75+
count[num] = 1;
76+
}
77+
}
78+
79+
for (int i = 1; i <= nums.length; i++) {
80+
if (count[i] == 0) {
81+
return i;
82+
}
83+
}
84+
85+
return nums.length + 1;
86+
}
87+
88+
private static int firstMissingPositiveSwapping(int[] nums) {
89+
int n = nums.length;
90+
91+
for (int i = 0; i < n; i++) {
92+
int position = nums[i] - 1;
93+
while (((position >= 0) && (position < n)) && (nums[i] != nums[position])) {
94+
int temp = nums[i];
95+
nums[i] = nums[position];
96+
nums[position] = temp;
97+
position = nums[i] - 1;
98+
}
99+
}
100+
101+
for (int i = 0; i < n; i++) {
102+
if (i != (nums[i] - 1)) {
103+
return i + 1;
104+
}
105+
}
106+
107+
return n + 1;
108+
}
109+
110+
private static int firstMissingPositiveNegation(int[] nums) {
111+
int n = nums.length;
112+
113+
for (int i = 0; i < n; i++) {
114+
if (nums[i] <= 0) {
115+
nums[i] = 0;
116+
}
117+
}
118+
119+
for (int num : nums) {
120+
num = Math.abs(num);
121+
int index = num - 1;
122+
if ((index >= 0) && (index < n)) {
123+
if (nums[index] == 0) {
124+
nums[index] = -(n + 1);
125+
} else if (nums[index] > 0) {
126+
nums[index] = -nums[index];
127+
}
128+
}
129+
}
130+
131+
for (int i = 1; i <= n; i++) {
132+
int index = i - 1;
133+
if (nums[index] >= 0) {
134+
return i;
135+
}
136+
}
137+
138+
return n + 1;
139+
}
140+
}

0 commit comments

Comments
 (0)