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

Your algorithm should run in O(n) time and use constant extra space.

Examples

Example 1:

Input: nums = [1, 2, 0] Output: 3 Explanation: The numbers 1 and 2 are present, so the first missing positive is 3.

Example 2:

Input: nums = [3, 4, -1, 1] Output: 2 Explanation: 1 is present but 2 is missing, so the answer is 2.

Example 3:

Input: nums = [7, 8, 9, 11, 12] Output: 1 Explanation: No positive integers starting from 1 are present, so the answer is 1.

Example 4:

Input: nums = [1, 2, 3, 4, 5] Output: 6 Explanation: All numbers from 1 to 5 are present, so the first missing positive is 6.

Example 5:

Input: nums = [] Output: 1 Explanation: An empty list has no positive integers, so the answer is 1.
Apex Code Editor

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay Jan 2026

Contest runs January 1 - 31. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.