Find First K Missing Positive Numbers

IF
AlgoAxiomStaff Engineers
JSTS
Medium20 mins

Given an unsorted array of integers nums and a positive integer k, return the first `k` missing positive integers that are not present in nums.

The result should be returned in ascending order.

Examples

Example 1:

Input: nums = [3, -1, 4, 5, 5], k = 3

Output: [1, 2, 6]

Explanation: Missing positives in order: 1, 2, 6, 7... The first 3 are [1, 2, 6].

Example 2:

Input: nums = [2, 3, 4], k = 3

Output: [1, 5, 6]

Explanation: Missing positives: 1, 5, 6, 7... The first 3 are [1, 5, 6].

Example 3:

Input: nums = [1, 2, 3, 4], k = 2

Output: [5, 6]

Explanation: All of [1..4] are present, missing starts at 5.

Constraints

  • 1 <= nums.length <= 10^4
  • -10^5 <= nums[i] <= 10^5
  • 1 <= k <= 50
Source: Cyclic Sort pattern — AlgoAxiom
JavaScript
Test Case 1
root = [1, 2, 3]
Test Case 2
root = [1, 2, 3, 4, 5]
Idle