Z11

First Missing Positive

Staff / Principal Engineer
Target Time: 40 - 50 Minutes
Type: NUMERIC

Problem Statement & Objective

Given an unsorted integer array nums, return the smallest positive integer that is not present in nums. You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.

Function Signature (Python)

first_missing_positive(nums: list[int]) -> int

Input & Output Contract

Output Rule:Smallest positive integer missing from the array.

System Constraints

1 <= nums.length <= 10^5
-2^31 <= nums[i] <= 2^31 - 1

Example Execution Walkthrough

nums = [3, 4, -1, 1]
result: 2
nums = [1, 2, 0]
result: 3

Example Test Scenarios (3)

Example 1
Input:[[3,4,-1,1]]
Expected Output:2
Example 2: 1 ve 2 Mevcut
Input:[[1,2,0]]
Expected Output:3
Example 3: 1 Eksik
Input:[[7,8,9,11,12]]
Expected Output:1
Loading Python Editor...
Input Parameters
[
  [
    3,
    4,
    -1,
    1
  ]
]
Expected Output
2