Lessons
Arrays
- Two Sum Problem with Solution
- Best Time to Buy and Sell Stock
- Array Contains Duplicates
- Product of Array Except Self: Optimized Approach
- Maximum Subarray Problem
- Maximum Product Subarray
- Find Minimum in Rotated Sorted Array
- Search in Rotated Sorted Array
- Container With Most Water
- Verifying an Alien Dictionary
- Next Permutation
- Remove Duplicates from Sorted Array
- Find First and Last Position of Element in Sorted Array
- Trapping Rain Water
- Median of Two Sorted Arrays
Dynamic Programming
- Climbing Stairs Problem
- Coin Change Problem
- Longest Increasing Subsequence
- Longest Common Subsequence (LCS)
- Word Break Problem
- Combination Sum Problem
- House Robber Problem
- Decode Ways Problem
- Unique Paths Problem
- Pascal's Triangle Problem
- Generate Parentheses Problem
- Jump Game with Dynamic Programming and Greedy Algorithms
- Regular Expression Matching
- Race Car Problem
Graph
First Missing Positive
When you’re handed an unsorted array and asked to find the smallest positive integer that’s missing, it might seem simple at first. But when efficiency matters, brute-force or sorting isn’t enough. This problem is not just about numbers—it's about smart array manipulation, understanding integer sequence, and using the right data structure.
What is the “First Missing Positive” Problem?
You’re given an unsorted array of integers. Your job? Find the first missing positive—the smallest positive integer that isn’t present in the array.
Example: For [3, 4, -1, 1]
, the answer is 2
.
This is not just a coding question—it teaches you how to reason about integer continuity, missing numbers, and optimal index finding.
Why Not Just Sort the Array?
You might think of sorting the array and scanning for gaps. But this approach comes with extra time—array sorting is O(n log n)
, which is not ideal when we’re aiming for O(n)
.
We’re not just solving for correctness—we’re solving for algorithm complexity and search performance.
How Array Manipulation Solves It Faster
Let’s think about this: In a perfect world, every positive number x
would be sitting at index x - 1
.
So:
1
should be at index0
2
at index1
- And so on…
By doing in-place array manipulation, we can try to put every number in its “ideal” index. Once that's done, we just scan through and check which position breaks this integer continuity.
Step-by-Step Approach
1. Clean the Input
Ignore all numbers that are:
- Negative
- Zero
- Greater than the array length (they don’t affect the answer)
2. Place Numbers in Correct Index
Use swapping to move each number x
to index x - 1
. This helps us keep positive integer identification fast and constant-time.
python
1 2 3 4 5 6 7 8 9 10
def firstMissingPositive(nums): n = len(nums) for i in range(n): while 1 <= nums[i] <= n and nums[nums[i]-1] != nums[i]: nums[nums[i]-1], nums[i] = nums[i], nums[nums[i]-1] for i in range(n): if nums[i] != i + 1: return i + 1 return n + 1
Why This Works
This method doesn't rely on any extra data structure. It only reuses the array space. You don’t need hashing, sorting, or nested loops. It’s all about index finding and maintaining integer continuity.
Algorithm Complexity Analysis
- Time Complexity:
O(n)
– Because each element is swapped at most once. - Space Complexity:
O(1)
– It’s in-place, no need for extra storage.
This makes it one of the most optimal solutions for this type of missing number problem.
Other Solutions – and Why We Avoid Them
1. Using HashSet
You can use a set to store all values and iterate from 1
to n
. But it needs extra space.
2. Sorting First
Sorting and scanning for gaps works but takes longer—O(n log n)
.
Our Approach Wins Because...
- It uses zero extra space
- Keeps everything in linear time
- Works cleanly even for edge cases like
[7,8,9,11,12]
Conclusion
Solving the First Missing Positive problem is more than a coding exercise—it’s a great example of how smart array manipulation, tight control over algorithm complexity, and a deep understanding of integer sequence help craft elegant solutions.
This approach helps you master:
- Positive integer identification
- Data structure logic
- Optimized index finding
- Real-world efficient integer continuity analysis
If you want to build confidence in coding interviews and write scalable solutions, this is the kind of problem you need to understand from the inside out.