First Missing Positive in Python

Published:5 min read

First Missing Positive in Array

Finding the first missing positive number in an array is a common question in coding interviews. The goal is to identify the smallest positive number that does not appear in the array. The challenge is to solve this efficiently without sorting the array or using extra space. This problem helps improve your skills in array manipulation and logical thinking. Want to learn how to solve it step by step? Let’s begin!

Steps to Solve First Missing Positive in Array

  • Understand the Problem:
    • You need to find the smallest positive number that is missing from the array.
  • Clean the Array:
    • Replace numbers that are negative or greater than the length of the array with a placeholder, like 0 or n+1.
  • Mark Numbers in Place:
    • Use the array itself to mark the presence of numbers:
      • For every number, mark its corresponding index (e.g., for number 3, mark index 2) by making the value at that index negative.
  • Find the Missing Positive:
    • Loop through the array. The first index with a positive value shows the missing number (index + 1).
  • Handle Edge Cases:
    • If all numbers from 1 to n are present, the missing number will be n + 1.
    • Handle arrays with only negatives or zeros.
  • Test Your Code:
    • Try examples like [3, 4, -1, 1] (output: 2) or [1, 2, 3] (output: 4).
First Missing Positive

The First Missing Positive problem is a common coding challenge where you need to find the smallest missing positive integer from an unsorted list of integers. This problem tests your ability to manipulate arrays and use optimal algorithms to solve problems efficiently.

Problem statement

Given an unsorted array of integers, find the smallest missing positive integer.

Example

python
1
2
nums = [1, 2, 0]
# Output: 3

Explanation: The smallest missing positive integer in the array is 3.

How to solve

To solve the First Missing Positive problem efficiently, we can use an algorithm with O(n) time complexity and O(1) space complexity (excluding the input array). The idea is to place each number in its corresponding index position (i.e., 1 should be at index 0, 2 should be at index 1, and so on). After this arrangement, the first index that does not match its value will give the smallest missing positive integer.

Pseudo code

Here is the pseudo code to solve the First Missing Positive problem:

  • Iterate through the array.
  • For each number, place it in its correct index position.
  • Iterate through the array again to find the first incorrect position.
  • Return the missing positive integer.

Full code in Python

Here's the complete Python code to solve the First Missing Positive problem:

python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def firstMissingPositive(nums):
    n = len(nums)
    
    # Place each number in its correct position
    for i in range(n):
        while 1 <= nums[i] <= n and nums[nums[i] - 1] != nums[i]:
            nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1]
    
    # Find the first missing positive
    for i in range(n):
        if nums[i] != i + 1:
            return i + 1
    
    return n + 1

# Example usage
print(firstMissingPositive([1, 2, 0]))  # Output: 3
print(firstMissingPositive([3, 4, -1, 1]))  # Output: 2
print(firstMissingPositive([7, 8, 9, 11, 12]))  # Output: 1

Time complexity

The time complexity of this solution is O(n), where n is the number of elements in the array. This is because each element is placed in its correct position at most once.

Frequently Asked Questions

To find missing positive numbers in an array, one common approach is to first place each positive integer at the index corresponding to its value (e.g., the value 1 should be at index 0). After rearranging the array, the first index that does not have the correct value will give you the first missing positive number. This method works efficiently in O(n) time with O(1) extra space.

To find the first missing positive number in an array, you can use the cyclic sort method. Rearrange the array so that each positive number x is placed at the index x-1. After rearrangement, iterate through the array to find the first index i where the value at that index is not i+1. The first such index i indicates that the missing number is i+1.

To find the smallest positive number in an array, you can iterate through the array while keeping track of the minimum positive value found. If you need to find the first missing positive number, you can use sorting or the cyclic sort method to identify the smallest positive integer that is not present in the array.

The first positive integer is 1. In the context of finding missing positive numbers in an array, the search typically begins with 1, as it is the smallest possible positive integer. If 1 is missing from the array, it is identified as the first missing positive integer.

Still have questions?Contact our support team

Related Articles

Sign-in First to Add Comment

Leave a comment 💬

All Comments

No comments yet.