First Missing Positive Integer
Given an array of integers, find the smallest missing positive integer.
We'll cover the following...
Statement
Given an unsorted integer array, nums
, find the smallest positive integer that is missing from the array.
Implement a solution that takes time and constant space.
Examples
Let’s look at some arrays and the first missing positive integer in each:
Sample input
[1, 9, 14, 11, 7, 13]
Expected output
2
Try it yourself
#include <iostream>#include <vector>using namespace std;int FirstMissingPositive(vector<int> nums) {// Write your code herereturn -1;}
Solution
First, we check the base case. To verify that the first missing positive integer is not 1
, we check for its presence in the array. We use the array below as an example. Since 1
exists in the array, it’s not our missing positive integer:
...