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.
Example 1:
Input: nums = [1,2,0] Output: 3 Explanation: The numbers in the range [1,2] are all in the array.
Example 2:
Input: nums = [3,4,-1,1] Output: 2 Explanation: 1 is in the array but 2 is missing.
Example 3:
Input: nums = [7,8,9,11,12] Output: 1 Explanation: The smallest positive integer 1 is missing.
Constraints:
1 <= nums.length <= 105-231 <= nums[i] <= 231 - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the very first positive number that is missing from a collection, the brute force strategy is to test numbers starting from the absolute beginning and check if each one is present.
Here's how the algorithm would work step-by-step:
def first_missing_positive(number_collection): # We must check every positive integer starting from one to find the first absent value current_target = 1 while True: # We assume the target is missing until we find evidence of it in the collection is_target_present = False for current_number in number_collection: if current_number == current_target: is_target_present = True break # If the target was never found during our scan, we have located our missing positive if not is_target_present: return current_target current_target += 1The optimal strategy is like playing a game of musical chairs where every positive number gets its own rightful seat based on its value. By rearranging the numbers so that each positive number sits in the position matching its value, the missing number will immediately reveal itself by sitting in the wrong spot.
Here's how the algorithm would work step-by-step:
def first_missing_positive(input_numbers): array_length = len(input_numbers) index_pointer = 0 while index_pointer < array_length: target_index = input_numbers[index_pointer] - 1 # We must place positive numbers in their exact index matching value minus one to enable O(1) space tracking. if 0 <= target_index < array_length and input_numbers[index_pointer] != input_numbers[target_index]: input_numbers[index_pointer], input_numbers[target_index] = input_numbers[target_index], input_numbers[index_pointer] else: index_pointer += 1 scan_pointer = 0 while scan_pointer < array_length: # The first position whose value does not match the expected sequence reveals the missing positive integer. if input_numbers[scan_pointer] != scan_pointer + 1: return scan_pointer + 1 scan_pointer += 1 # If all positions contain their expected values, the missing positive is the next integer after the array length. return array_length + 1| Case | How to Handle |
|---|---|
| Empty array or null input | Return 1 immediately since the smallest missing positive integer in an empty set is always 1. |
| Array with a single element like 1 or 2 | If the element is 1, return 2; otherwise, return 1 as the smallest positive integer. |
| Array containing only negative numbers and zeros | The algorithm will ignore these non-positive values and correctly return 1 as the first missing positive. |
| Array already containing a continuous sequence starting from 1 up to N | The in-place cyclic sort will place each number in its correct bucket, leading to N + 1 as the output. |
| Array with large positive numbers far exceeding the array length | Values greater than the array length are ignored during placement because they cannot affect the smallest missing positive within range 1 to N + 1. |
| Array containing duplicate numbers | The cyclic placement handles duplicates gracefully by ignoring redundant values once the correct position is already occupied. |
| Maximum-sized inputs up to integer limits | The linear time O(N) and constant space O(1) in-place modification ensures the solution scales efficiently without exceeding memory constraints. |
| Inputs with extreme boundary values including Integer.MAX_VALUE and Integer.MIN_VALUE | Out-of-bound values are filtered out safely so they do not cause array index out of bounds exceptions. |