Here is the interview question prompt, presented for reference.
We have an unsorted array of integers such as the following:
[0, 3, -1, -2, 1]
if (len == 2) return Math.Max(nums[0], nums[1]);
In the above example, the minimum number is -2 and the maximum is 3. If it were sorted, it would look like:
[-2, -1, 0, 1, 3]
if (len == 2) return Math.Max(nums[0], nums[1]);
This means there is an expected range (defined as the collection of values between the minimum and maximum values) of [-2, -1, 0, 1, 2, 3] for the array.
But look at the example again:
[-2, -1, 0, 1, 3]
if (len == 2) return Math.Max(nums[0], nums[1]);
We're missing a number: 2. The smallest missing positive integer for our input array is 2.
Can you write a method that takes such an array and returns the first missing positive integer?
leastMissingPositive([1, 2, 3, 0])
// 4
if(len == 2) return Math.Max(nums[0], nums[1]);
In the above example, it is 4 since we already have 0 through 3. Have your code run in O(n) time with constant space (hence, we can easily determine if it was sorted, but most sorts will take O(n * log n) time).
100000-100000 and 100000O(n)O(1)You can see the full challenge with visuals at this link.
Challenges • Asked over 5 years ago by Anonymous
This is the main discussion thread generated for Least Missing Positive Number.
[deleted]
this question is very ambiguous in its wording
Hi, happy to help. Which part is ambiguous?
Why is the return expected to be 2 in the first test case and not 0?
Hi, Jake!
Very Interesting problem with very tricky solution
I have a question though. Why we have inner while-loop instead just if. Seems that we never do greater then just only one swap for each index i. Can you please demonstrate example when input cause inner while-loop iterates several times for some index i