Problem List

First Missing Positive

May 31, 2025Go arrayhard

Problem

Approach

Reflections

This problem was my first hard and honestly easier than some of the easy's. Also the reminder that O(n) = O(2n) was a massive hint.

Performance

Complexity

Go Solution
func firstMissingPositive(nums []int) int {
	for i := 0; i < len(nums); i++ {
		for 1 <= nums[i] && nums[i] <= len(nums) && nums[i] != i+1 && nums[nums[i]-1] != nums[i] {
			nums[i], nums[nums[i]-1] = nums[nums[i]-1], nums[i]
		}
	}

	for i, num := range nums {
		if num != i+1 {
			return i + 1
		}
	}

	return len(nums) + 1
}

LeetCode Problem Link