First Missing Positive
LeetCode 41 · Hard · solution, intuition & step-by-step walkthrough
Use indices as a presence map (sign marks presence).
This is how I solved First Missing Positive (LeetCode 41), a hard problem from the Cyclic Sort / Index Marking pattern. Below: the intuition, the approach step by step, my accepted Python code and the 2 versions I wrote on the way, animated test cases and a line-by-line debugger.
Intuition
The answer is always in 1..n+1, so only values in that range matter. A boolean table of size n+1 shows which are present. To do it in O(1) space, reuse the array itself: clamp junk values to 1, then mark value v as present by making nums[v] negative.
Approach
- 1
If 1 is missing, the answer is 1.
- 2
Replace values ≤ 0 or > n with 1 (harmless now).
- 3
For each value v, negate nums[v] (nums[0] stands for n).
- 4
The first index still positive is the answer; otherwise n or n+1.
How I solved it · from first idea to final
Boolean array of 1..n.
The array is its own hash table.
Presence tablePythonO(n) time · O(n) space
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
n=len(nums)
dp = [False]*(n+1)
for i in range(n):
if nums[i]>0 and nums[i]<n+1:
dp[nums[i]]=True
for i in range(1,n+1):
if dp[i]==False:
return i
return n+1In-place sign markingPythonO(n) time · O(1) space
class Solution:
def firstMissingPositive(self, nums: List[int]) -> int:
#using the same array
#use the index as key, if nums[num] exists it should be negative, else it should be positive
#scale the number between 1 to n
#nums[0]=-1 if n exists else 1
#before it check whether 1 exist or not
has_1 = False
n = len(nums)
for i in range(n):
if nums[i]==1:
has_1=True
elif nums[i]<=0 or nums[i]>n:
nums[i]=1
if not has_1:
return 1
for i in range(n):
num = abs(nums[i])
if num==n:
nums[0]=(-1)*abs(nums[0])
else:
nums[num]=(-1)*abs(nums[num])
for i in range(1,n):
if nums[i]>0:
return i
if nums[0]>0:
return n
return n+1First Missing Positive — FAQ
What is the intuition behind First Missing Positive?
The answer is always in 1..n+1, so only values in that range matter. A boolean table of size n+1 shows which are present. To do it in O(1) space, reuse the array itself: clamp junk values to 1, then mark value v as present by making nums[v] negative.
How do you solve First Missing Positive (LeetCode 41)?
1. If 1 is missing, the answer is 1. 2. Replace values ≤ 0 or > n with 1 (harmless now). 3. For each value v, negate nums[v] (nums[0] stands for n). 4. The first index still positive is the answer; otherwise n or n+1.
What is the time and space complexity of First Missing Positive?
Time: O(n). Space: O(1) in the final version. (Accepted approach: In-place sign marking.)
What are common mistakes in First Missing Positive?
Use abs() when reading values — they may already be negated.
Is there more than one way to solve First Missing Positive?
Presence table (O(n) time · O(n) space): Boolean array of 1..n. In-place sign marking (O(n) time · O(1) space): The array is its own hash table.