Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

41. First Missing Positive

Hard40.2% Acceptance
ArrayHash Table
Asked by:
A
Amazon
Microsoft
ProblemHints (3)Solutions (12)VideosCompanies (8)Notes

Problem Statement

Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums.

You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.

Example 1:

Input: nums = [1,2,0]
Output: 3
Explanation: The numbers in the range [1,2] are all in the array.

Example 2:

Input: nums = [3,4,-1,1]
Output: 2
Explanation: 1 is in the array but 2 is missing.

Example 3:

Input: nums = [7,8,9,11,12]
Output: 1
Explanation: The smallest positive integer 1 is missing.

Constraints:

  • 1 <= nums.length <= 105
  • -231 <= nums[i] <= 231 - 1
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
M
A
Adobe
G
Google
F
Facebook
+3

Approach

The key challenge in First Missing Positive is finding the smallest missing positive integer in O(n) time while using O(1) extra space. Since the answer must lie in the range 1 to n + 1 (where n is the array length), we can leverage the array itself to track which numbers exist.

A common strategy is an index placement (cyclic sort–style) approach. The idea is to place each value x at index x - 1 whenever 1 ≤ x ≤ n. By repeatedly swapping elements into their correct positions, the array becomes a representation of which positives are present. A final scan reveals the first index where nums[i] != i + 1, which corresponds to the missing positive.

An alternative approach uses a hash set to record all positive numbers and then checks sequentially from 1 upward. While simpler conceptually, it requires additional memory. The in-place method is preferred in interviews because it achieves O(n) time with constant extra space.

Complexity

ApproachTime ComplexitySpace Complexity
In-place index placement (cyclic positioning)O(n)O(1)
Hash Set trackingO(n)O(n)

Video Solution Available

NeetCode

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Think about how you would solve the problem in non-constant space. Can you apply that logic to the existing space?

2
Hint 2

We don't care about duplicates or non-positive integers

3
Hint 3

Remember that O(2n) = O(n)

Ready to see the solutions?View Solutions

Solutions (12)

Cyclic Sort

This approach utilizes the cyclic sort algorithm to place numbers in their corresponding indices. For example, 1 should be at index 0, 2 should be at index 1, etc. While rearranging, we ignore numbers outside the range [1, n], where n is the length of the array.

After rearranging, the first index i that doesn’t have a number i+1 indicates the smallest missing positive integer.

Time Complexity: O(n), as each number is swapped at most once.
Space Complexity: O(1), as no additional space is used apart from variables.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3int firstMissingPositive(int* nums, int numsSize) {
4    for (int i = 0; i < numsSize;

Explanation

The function iterates over the array, swapping elements within the valid range to their correct positions. It then checks for the first missing positive by assessing incorrect positions.

Hash Concept with Array

By assuming the input array itself can act like a hash map, this approach assigns each index with a corresponding positive value within the range. If out of range, we fill that index with a placeholder number like the array size + 1.

We then use index signs to mark present numbers and deduce the missing positive from the invalid marked position.

Time Complexity: O(n)
Space Complexity: O(1)

CC++JavaPythonC#JavaScript
1#

Video Solutions

Watch expert explanations and walkthroughs

First Missing Positive - Leetcode 41 - Python

NeetCode
21:22127,454 views

Asked By Companies

8 companies
A
Amazon
M
Microsoft
A
Adobe
G
Google
F
Facebook
D
Databricks
G
Grab
W
Walmart Global Tech

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Two SumEasy
Valid SudokuMedium
Sudoku SolverHard
Group AnagramsMedium
More similar problems

Related Topics

ArrayHash Table

Problem Stats

Acceptance Rate40.2%
DifficultyHard
Companies8

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why does the answer lie between 1 and n+1?

If an array has length n, it can contain at most n distinct positive numbers from the range 1 to n. If all numbers from 1 to n appear, the smallest missing positive must be n+1. This observation helps narrow the search space and enables efficient solutions.

Is First Missing Positive asked in FAANG interviews?

Yes, First Missing Positive is a well-known hard interview problem and has appeared in FAANG-style interviews. It tests array manipulation, index mapping, and the ability to design in-place algorithms with strict time and space constraints.

What data structure is best for First Missing Positive?

The most efficient solution relies directly on the input array as a placement structure rather than using additional data structures. However, a hash set can also be used to track seen positive numbers, which simplifies the logic but requires O(n) extra space.

What is the optimal approach for First Missing Positive?

The optimal approach rearranges elements so each number x is placed at index x-1 when possible. After this in-place placement, a linear scan identifies the first index where the value does not match its expected position. This achieves O(n) time and O(1) extra space.

Previous Problem

Combination Sum II

Next Problem

Trapping Rain Water

i
++
)
{
5
while
(
nums
[
i
]
>
0
&&
nums
[
i
]
<=
numsSize
&&
nums
[
nums
[
i
]
-
1
]
!=
nums
[
i
]
)
{
6
int
temp
=
nums
[
nums
[
i
]
-
1
]
;
7
nums
[
nums
[
i
]
-
1
]
=
nums
[
i
]
;
8
nums
[
i
]
=
temp
;
9
}
10
}
11
for
(
int
i
=
0
;
i
<
numsSize
;
i
++
)
{
12
if
(
nums
[
i
]
!=
i
+
1
)
{
13
return
i
+
1
;
14
}
15
}
16
return
numsSize
+
1
;
17
}
18
19
int
main
(
)
{
20
int
nums
[
]
=
{
3
,
4
,
-
1
,
1
}
;
21
int
size
=
sizeof
(
nums
)
/
sizeof
(
nums
[
0
]
)
;
22
int
result
=
firstMissingPositive
(
nums
,
size
)
;
23
printf
(
"The first missing positive is %d\n"
,
result
)
;
24
return
0
;
25
}
include
<stdio.h>
2
#
include
<stdlib.h>
3
4
int
firstMissingPositive
(
int
*
nums
,
int
numsSize
)
{
5
for
(
int
i
=
0
;
i
<
numsSize
;
i
++
)
{
6
if
(
nums
[
i
]
<=
0
||
nums
[
i
]
>
numsSize
)
{
7
nums
[
i
]
=
numsSize
+
1
;
8
}
9
}
10
for
(
int
i
=
0
;
i
<
numsSize
;
i
++
)
{
11
int
num
=
abs
(
nums
[
i
]
)
;
12
if
(
num
<=
numsSize
)
{
13
nums
[
num
-
1
]
=
-
abs
(
nums
[
num
-
1
]
)
;
14
}
15
}
16
for
(
int
i
=
0
;
i
<
numsSize
;
i
++
)
{
17
if
(
nums
[
i
]
>
0
)
{
18
return
i
+
1
;
19
}
20
}
21
return
numsSize
+
1
;
22
}
23
24
int
main
(
)
{
25
int
nums
[
]
=
{
3
,
4
,
-
1
,
1
}
;
26
int
size
=
sizeof
(
nums
)
/
sizeof
(
nums
[
0
]
)
;
27
int
result
=
firstMissingPositive
(
nums
,
size
)
;
28
printf
(
"The first missing positive is %d\n"
,
result
)
;
29
return
0
;
30
}

Explanation

Remove out of range numbers by filling them with the max possible value. Using negative marking helps identify seen numbers within the range, returning the first positive marked index.