Grokking the Coding Interview: Patterns for Coding Questions

0% completed

Solution: Problem Challenge 2: Find the Smallest Missing Positive Number

Problem Statement

Given an unsorted array containing numbers, find the smallest missing positive number in it.

Note: Positive numbers start from '1'.

Example 1:

Input: [-3, 1, 5, 4, 2]
Output: 3
Explanation: The smallest missing positive number is '3'

Example 2:

Input: [3, -2, 0, 1, 2]
Output: 4

Example 3:

Input: [3, 2, 5, 1]
Output: 4

Example 4:

Input: [33, 37, 5]
Output: 1

Constraints:

  • 1 <= nums.length <= 10^5
  • -2<sup>31</sup> <= nums[i] <= 2<sup>31</sup> - 1

.....

.....

.....

Like the course? Get enrolled and start learning!
M

Mandy Liu

· 3 years ago

class Solution: def findNumber(self, nums): smallest = 1 for num in nums: if smallest == num: smallest += 1 return smallest
Show 2 replies
R

Ryun Kim

· 4 years ago

Some of these cyclic sort algorithms don't work on the corresponding LeetCode problems...

Show 1 reply
G

Gary

· 4 years ago

Don't understand why it returns nums.length + 1 at the end? For which input would this line be needed?

Show 2 replies
S

Shan

· 4 years ago

There is a slight typo in here. Take line 3 inside the function for java.

"if (nums[i] > 0 && nums[i]