Kth Missing Positive Number
Easy
Topics
Given a strictly increasing array of positive integers nums and an integer target (= k), return the target-th positive integer that is missing from the array.
Example 1
Input: nums = [2,3,4,7,11], target = 5 Output: 9
Example 2
Input: nums = [1,2,3,4], target = 2 Output: 6
Constraints
- 1 <= nums.length <= 1000
- 1 <= nums[i] <= 1000
- nums is strictly increasing.
- 1 <= target <= 1000
Editorial — intuition, approach, complexity and pitfalls
Intuition
Walking the array and counting gaps is O(n) and entirely acceptable. The reason to do better is that the array is strictly increasing, which makes the number of missing values monotonic — and anything monotonic can be binary searched.
The key quantity: at index i, the count of missing positives before nums[i] is nums[i] - (i + 1). If no positives were missing, nums[i] would be exactly i+1; the excess is precisely how many were skipped.
That count never decreases as i grows, so we binary search for the first index where it reaches k. Everything left of that index has fewer than k missing, so the answer lies in the gap at that boundary — and since lo values of the array sit below it, the answer is k + lo.
Approach
- Binary search over indices with
lo = 0,hi = len(nums). - If
nums[mid] - (mid + 1) < k, the k-th missing is further right:lo = mid + 1. Elsehi = mid. - Return
k + lo.
hi starts at len(nums), not len(nums) - 1, because the answer may lie beyond the last element.
Dry Run
nums = [2,3,4,7,11], k = 5
missing counts: nums[i] - (i+1) = [1,1,1,3,6]
lo=0 hi=5 mid=2 count=1 < 5 -> lo=3
lo=3 hi=5 mid=4 count=6 >= 5 -> hi=4
lo=3 hi=4 mid=3 count=3 < 5 -> lo=4
lo == hi == 4, stop
answer k + lo = 5 + 4 = 9
check: missing positives are 1,5,6,8,9,10,... the 5th is 9 ✓Complexity
- Time — O(log n): one binary search, constant work per probe. The linear scan is O(n + k).
- Space — O(1).
Common Pitfalls
- Setting
hi = len(nums) - 1. When the answer lies past the last element — as it does for a largek— the search cannot reach it. - Getting the missing-count formula off by one. It is
nums[i] - (i + 1), because indexiwould holdi+1in a gapless array. - Mixing loop shapes: this one uses
while lo < hiwithhi = mid, which converges on the boundary. Pairing it withhi = mid - 1loses the answer. - Returning
nums[lo] - 1or similar. The answer isk + lo, derived from how many array values sit below it. - Using
<=in the comparison, which lands on the last index with that count rather than the first.
Full Solution — all languages
class Solution:
def findKthPositive(self, nums, target):
lo, hi = 0, len(nums)
while lo < hi:
mid = (lo + hi) // 2
if nums[mid] - (mid + 1) < target:
lo = mid + 1
else:
hi = mid
return target + loimport java.util.*;
class Solution {
public int findKthPositive(int[] nums, int target) {
int lo = 0, hi = nums.length;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (nums[mid] - (mid + 1) < target) lo = mid + 1;
else hi = mid;
}
return target + lo;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int findKthPositive(vector<int>& nums, int target) {
int lo = 0, hi = (int)nums.size();
while (lo < hi) {
int mid = (lo + hi) / 2;
if (nums[mid] - (mid + 1) < target) lo = mid + 1;
else hi = mid;
}
return target + lo;
}
};/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/ var findKthPositive = function(nums, target) {
let lo = 0, hi = nums.length;
while (lo < hi) {
const mid = (lo + hi) >> 1;
if (nums[mid] - (mid + 1) < target) lo = mid + 1;
else hi = mid;
}
return target + lo;
};