Program to find lowest possible integer that is missing in the array in Python

Finding the first missing positive integer is a common programming problem. Given an array that may contain duplicates and negative numbers, we need to find the lowest positive number that is not present in the array.

So, if the input is like nums = [0,3,1], then the output will be 2 because 1 and 3 are present, but 2 is missing.

Algorithm Approach

To solve this problem, we follow these steps:

  • Create a set containing only positive numbers from the input array

  • If the set is empty (no positive numbers), return 1

  • Iterate from 1 to len(set) + 2 and return the first number not in the set

Implementation

Here's the complete solution ?

class Solution:
    def solve(self, nums):
        # Create a set with only positive numbers
        positive_nums = set(num for num in nums if num > 0)
        
        # If no positive numbers exist, return 1
        if not positive_nums:
            return 1
            
        # Find the first missing positive integer
        for i in range(1, len(positive_nums) + 2):
            if i not in positive_nums:
                return i

# Test the solution
ob = Solution()
nums = [0, 3, 1]
result = ob.solve(nums)
print(f"Input: {nums}")
print(f"First missing positive: {result}")

The output of the above code is ?

Input: [0, 3, 1]
First missing positive: 2

Additional Examples

Let's test with different input scenarios ?

class Solution:
    def solve(self, nums):
        positive_nums = set(num for num in nums if num > 0)
        
        if not positive_nums:
            return 1
            
        for i in range(1, len(positive_nums) + 2):
            if i not in positive_nums:
                return i

ob = Solution()

# Test case 1: Array with negatives and zero
test1 = [-1, -2, 0]
print(f"Input: {test1}, Output: {ob.solve(test1)}")

# Test case 2: Consecutive positive numbers
test2 = [1, 2, 3, 4]
print(f"Input: {test2}, Output: {ob.solve(test2)}")

# Test case 3: Array with duplicates
test3 = [3, 4, -1, 1, 1, 2]
print(f"Input: {test3}, Output: {ob.solve(test3)}")

The output shows different scenarios ?

Input: [-1, -2, 0], Output: 1
Input: [1, 2, 3, 4], Output: 5
Input: [3, 4, -1, 1, 1, 2], Output: 5

How It Works

The algorithm works by:

  • Filtering: We create a set containing only positive numbers, which automatically removes duplicates and negatives

  • Edge case: If no positive numbers exist, the first missing positive is 1

  • Sequential search: We check numbers starting from 1 until we find one not in our set

  • Range optimization: We only need to check up to len(set) + 2 because the answer cannot be larger than that

Time and Space Complexity

  • Time Complexity: O(n) where n is the length of the input array

  • Space Complexity: O(k) where k is the number of unique positive integers in the array

Conclusion

This solution efficiently finds the first missing positive integer by filtering positive numbers into a set and performing a sequential search. The approach handles edge cases like empty arrays, negative numbers, and duplicates effectively.

Updated on: 2026-03-25T11:08:20+05:30

253 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements