First Missing Positive
HardAcc. 42.1%
+100 XP 50
Expert Optimization
Find the smallest missing number in O(n) time and O(1) space.
The Assignment
Your function receives nums.
- Cycle Sort: Move each number
xto its correct indexx-1(ifxis between 1 and N). - After the sort, iterate again and find the first index where
nums[i] !== i+1. - Print
i+1.
01EXAMPLE 1
Input
[3, 4, -1, 1]Output
2Explanation: 1 is there, 2 is missing.
Constraints
- Strict O(n) time.
- Strict O(1) space.
ArraysAlgorithms
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
nums[3,4,-1,1]
Expected Output
2
Click RUN to test your solution