VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-smallest-positive-number-missing-from-an-unsorted-array/

⇱ Smallest Missing Positive Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Smallest Missing Positive Number

Last Updated : 23 Jul, 2025

Given an unsorted array arr[] with both positive and negative elements, find the smallest positive number missing from the array.

Examples:

Input: arr[] = [2, -3, 4, 1, 1, 7]
Output: 3
Explanation: 3 is the smallest positive number missing from the array.

Input: arr[] = [5, 3, 2, 5, 1]
Output: 4
Explanation: 4 is the smallest positive number missing from the array.

Input: arr[] = [-8, 0, -1, -4, -3]
Output: 1
Explanation: 1 is the smallest positive number missing from the array.

[Naive approach] By Sorting - O(n*log n) Time and O(1) Space

The idea is to sort the array and assume the missing number as 1. Now, iterate over the array and for each element arr[i],

  • If arr[i] == missing number, then increment missing number by 1.
  • If arr[i] < missing number, then continue to search for the missing number.
  • If arr[i] > missing number, then break and return the missing number

Output
3

[Better approach] Using Visited Array - O(n) Time and O(n) Space

The idea is to create a visited array, to keep track of which numbers from the original array were present. For each positive number in the input array, we mark its corresponding position in the visited array. After that, we go through the visited array to find the first position that isn’t visited. The first unvisited index tells us the first missing positive number.
Note that we are marking numbers within the range from 1 to n only.


Output
3

[Expected Approach] Using Cycle Sort - O(n) Time and O(1) Space

The idea is similar to Cycle Sort and move each element in the array to its correct position based on its value. So for each number, say x, such that 1 ≤ x ≤ n, is placed at the (x - 1)th index.

Finally, iterate through the array and check if the numbers are in the expected indexes or not. The first place where the number doesn’t match its index gives us the first missing positive number. If all the numbers from 1 to n, are at their correct indexes, then the next number i.e., n + 1, is the smallest missing positive number.


Output
3

[Alternate Approach - 1] By Negating Array Elements – O(n) Time and O(1) Space

The main idea of this approach is to first move all positive integers to the left side of the array. Then, for the left segment, it marks the presence of a number val (where 1 ≤ val ≤ k) by making the element at index (val - 1) negative. This marking helps identify which numbers are present. Finally, it scans the marked segment to find the first index that remains positive, indicating the smallest missing positive number. If all positions are marked, the missing number is k + 1.


Output
3

[Alternate Approach - 2] By Marking Indices – O(n) Time and O(1) Space

The idea is to first check if 1 is present in the array or not. If not, the answer is 1. Otherwise, replace all the numbers outside the range [1, n] to 1. Then, iterate over the array again and mark the occurrences of each number, say x by adding n to index x - 1.Lastly, iterate over the array again to find the missing element by searching for the first unmarked index.


Output
3


Comment