First Missing Positive
Posted: 11 Sep, 2020
Difficulty: Moderate
You are given an array 'ARR' of integers of length N. Your task is to find the first missing positive integer in linear time and constant space. In other words, find the lowest positive integer that does not exist in the array. The array can have negative numbers as well.
For example, the input [3, 4, -1, 1] should give output 2 because it is the smallest positive number that is missing in the input array.
Input format:
The first line of input contains a single integer T, representing the number of test cases or queries to be run.
Then the T test cases follow.
The first line of each test case contains a positive integer N which represents the length of the array.
The second line of each test case contains N integers representing the elements of the array 'ARR'.
Output Format :
For each test case, print a single integer denoting the minimum positive integer that is missing from the given input array.
Th output of each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraint :
1 <= T <= 10
1 <= N <= 10^5
-10^5 <= ARR[i] <= 10^5
Time Limit: 1 sec
Approach 1
- Since we are given an integer ‘N’, there could be a maximum of ‘N’ positive integers in the array.
- The minimum positive number is 1, hence we can search from 1 to N in the array.
- Run a loop from 1 to N, and search for each element in the array.
- If the element is present in the array then search for the next element.
- If it is not present then, the element is the answer.
- Otherwise, if all the elements between 1 to N are present in the array then the minimum positive integer that is missing would be N+1. So the answer would be N+1.
Approach 2
- Sort the array.
- Initialize a variable counter with 1, that will keep track of the positive integers that are present in the array.
- Traverse the whole array and if the current element is negative or zero then skip the element.
- If the current element is positive and equal to the counter, then increment the counter.
- If the current element is not equal to the counter then return the counter, because the counter will be the smallest positive missing element.
- If the whole array is traversed then return N+1.
Approach 3
- With the help of extra space, we can mark all the positive elements present in the array.
- Traverse the array and store all the positive elements between 0 to N+1 of the array in the visited array.
- ARR[i] will be stored at VIS[ARR[i]].
- After storing all the positive elements in the visited array, run a loop from 1 to N+1 and check which smallest element is not present in the visited array.
- Return the element that is missing from the visited array.
Approach 4
- Call a function that will segregate the positive integer to the negative integers i.e move all non-positive elements to the right side, and return the index at which non-positive integers START.
- Now we can ignore non-positive elements and consider only the part of the array which contains all positive elements. We traverse the array containing all positive numbers. To mark the presence of an element ARR[i], we change the sign of value at index ARR[i] to negative i.e mark the presence of element 1 by making the element of array at index 1 to negative, given that the index lies in that segment of positive elements.
- To find the smallest positive missing element, we traverse the positive elements array again and print the first index which has positive value. In case all elements are negative, our index is SIZE - 1. We subtract 1 from the index and that would be the answer.