First Missing Positive - Complete Solution Guide
First Missing Positive is LeetCode problem 41, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem asks us to find the smallest positive integer that is missing from a given unsorted integer array `nums`. The solution must have a time complexity of O(n) and a space complexity of O(1). In simpler terms, given an array like [3, 4, -1, 1], we need to find the smallest positive number (1, 2, 3, ...) that's not in the array. In this case, it would be 2.
Solution Approach
The solution uses the array itself as a hash table. It iterates through the array and, for each element `nums[i]`, it attempts to place it at its correct index `nums[i] - 1`. If `nums[i]` is within the range [1, n] and `nums[i]` is not already at its correct position (i.e., `nums[i] != nums[nums[i] - 1]`), we swap `nums[i]` with the element at its correct index. This process is repeated until all elements are either in their correct positions or are out of range. Finally, we iterate through the rearranged array and return the first index `i` where `nums[i] != i + 1`. If no such index is found, it means all numbers from 1 to n are present, so the answer is n + 1.
Step-by-Step Algorithm
- Step 1: Iterate through the input array `nums` from index 0 to n-1.
- Step 2: For each element `nums[i]`, check if it falls within the range [1, n]. Also, check if `nums[i]` is already at its correct position i.e., check if `nums[i] == nums[nums[i] - 1]`.
- Step 3: If `nums[i]` satisfies the above conditions, swap `nums[i]` with the element at index `nums[i] - 1`. This places `nums[i]` at its correct position.
- Step 4: Repeat steps 2 and 3 until the current element `nums[i]` is either out of the valid range or is already at its correct position or it's equal to the value at its correct index.
- Step 5: Increment `i` to move to the next element in the array.
- Step 6: After the rearrangement, iterate through the array again from index 0 to n-1.
- Step 7: For each element `nums[i]`, check if `nums[i]` is equal to `i + 1`. If not, it means the number `i + 1` is missing, so return `i + 1`.
- Step 8: If the loop completes without finding a missing number, it means all numbers from 1 to n are present in the array. In this case, return `n + 1`.
Key Insights
- Insight 1: The missing positive number must be between 1 and n+1, where n is the length of the array. This is because if all numbers from 1 to n are present, the answer is n+1.
- Insight 2: We can use the array itself as a hash table by rearranging the elements such that `nums[i]` should ideally be `i+1`. This allows us to check for the missing number in O(n) time without using extra space.
- Insight 3: Pay attention to boundary conditions and avoid infinite loops during the rearrangement process. Ensure that only numbers within the range [1, n] are moved to their correct positions.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Hash Table.
Companies
Asked at: Adobe, Amazon, Apple, Bloomberg, Celigo, Databricks, EPAM Systems, Flipkart, Geico, General Motors, Goldman Sachs, Google, Licious, MakeMyTrip, Meta, Microsoft, Morgan Stanley, Myntra, Netflix, Nutanix, Oracle, PayPal, PhonePe, Roblox, Salesforce, Samsung, ServiceNow, Siemens, SoundHound, Sprinklr, Sumo Logic, Swiggy, Tesla, TikTok, Twilio, Uber, Walmart Labs, Zomato, athenahealth, eBay.