Smallest Missing Positive Number

You are given an unsorted integer array nums.

Your task is to find the smallest positive integer that does not appear in the array.

A positive integer means a number greater than 0.

You must solve this problem in:

  • O(n) time complexity
  • O(1) auxiliary space

Example 1:

Input: nums = [2, 1, 3]

Output: 4

Explanation:

The positive integers 1, 2, and 3 are present in the array.

So, the smallest positive integer missing from the array is 4.

Example 2:

Input: nums = [5, 1, -2, 2]

Output: 3

Explanation:

The array contains 1 and 2, but 3 is missing.

So, the smallest missing positive integer is 3.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [1, 2, 4]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

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

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
// Your code goes here
}
};
Test Case

Input:

Nums