Kth Missing Positive Number
Find the k-th positive number that is missing from a sorted array, without counting the gaps one by one.
You are given an array arrkarr
Example 1
Output: 9
Explanation: the missing numbers are 1, 5, 6, 8, 9, 10, 12 and so on; the 5th is 9.
Example 2
Output: 6
Explanation: nothing is missing up to 4, so the missing numbers are 5, 6, 7 and so on; the 2nd is 6.
The Idea
Write out the positive numbers 1, 2, 3 and so on, and cross off the ones that appear in the array. In Example 1 that crosses off 2, 3, 4, 7 and 11. The numbers left over, from the left, are 1, 5, 6, 8, 9, 10, then 12 and everything after it. The 5th of them is 9, the answer.
Finding 9 that way means walking through the numbers one at a time. That is fine for k = 5, but k could be a million. So can we tell how many numbers are missing at some point in the array, without counting them one by one?
We can. If nothing were missing, the array would start 1, 2, 3, and the value at index i, counting from 0, would be i + 1. Each missing number below a value pushes that value up by one. So arr[i] - (i + 1)arr[i]
The blue boxes are the values in arr
How do we get from there to 9 without counting? Every number from 1 up to 9 is either missing or in the array. Five of them are missing, up to and including 9 itself, and four are values of the array: 2, 3, 4 and 7. So the answer is 4 + 5 = 9, and that 4 is exactly the index of 11, the first value with at least five numbers missing below it.
So the whole problem comes down to one search: find the first index whose missing count reaches k, and add k to that index.
Solution Approach
Turning that idea into code means settling two things: how to find the first index whose missing count reaches k, and where that search may end.
Finding the first index that reaches k
How do we find that index quickly? The simple way walks the numbers 1, 2, 3 and so on, skips the ones in the array, and stops at the k-th missing one. That takes about n + k steps, where n is the length of the array, which is slow when k is large.
The missing counts help, because they never go down. The array is strictly increasing, so from one index to the next the value grows by at least 1, while i + 1 grows by exactly 1. The count therefore stays the same or grows. In the picture the counts run 1, 1, 1, 3, 6. In such a list, all the counts below k come first and all the others after them. So one look at the middle tells us which half still holds the first count that reaches k.
Keep a range of indexes, from lohimidmidlomid + 1midhimidlohi
In Example 1 the range starts as 0 to 5. It ends at 5, one past the last index, because the answer may lie after the last value. Index 2 has count 1, too few, so lohilohi
Where the search may end
What if no value in the array has k numbers missing below it? In Example 2, [1, 2, 3, 4]hiarr.lengthlo
An answer before the first value needs nothing special. For [5]lo
Java Solution
The method findKthPositive
- First, set up the range:
starts at 0 andlo athi , one past the last index, so the search can end after the last value.arr.length - While
is less thanlo , take the middle indexhi asmid . Written this way, the sumlo + (hi - lo) / 2 is never formed, so it cannot grow past the largestlo + hi . Then count the numbers missing belowint intoarr[mid] , asmissing .arr[mid] - (mid + 1) - If
is less thanmissing , the index we want is to the right, sok becomeslo . Otherwisemid + 1 becomeshi .mid - When the loop ends,
is the first index with at leastlo numbers missing below it, which is also how many values of the array are below the answer. Returnk .lo + k
The main
public class KthMissingPositiveNumber {
public static int findKthPositive(int[] arr, int k) {
int lo = 0;
int hi = arr.length; // the answer may lie past the last value
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
int missing = arr[mid] - (mid + 1); // positives missing below arr[mid]
if (missing < k) {
lo = mid + 1; // fewer than k: the answer is further right
} else {
hi = mid; // k or more: mid may be the first such index
}
}
return lo + k; // lo values are present below the answer
}
public static void main(String[] args) {
System.out.println(findKthPositive(new int[] {2, 3, 4, 7, 11}, 5));
System.out.println(findKthPositive(new int[] {1, 2, 3, 4}, 2));
System.out.println(findKthPositive(new int[] {5}, 1));
System.out.println(findKthPositive(new int[] {2, 3, 4, 7, 11}, 1000));
System.out.println(findKthPositive(new int[] {2, 3, 4, 7, 11}, 3));
}
}
Running it prints:
9
6
1
1005
6
Look at the second line, 6, from [1, 2, 3, 4]lo[2, 3, 4, 7, 11]
The third line, 1, comes from [5]lo
Starting with hi = arr.length - 1lo
Complexity
The only work is the binary search, and it never looks at the missing numbers one by one.
- Time: O(log n), where n is the length of the array. The range from
tolo starts with n + 1 positions, and each pass of the loop halves it. The size of k does not matter, unlike walking the numbers one at a time, which takes about n + k steps.hi - Space: O(1). The search keeps only
,lo ,hi andmid , and the array is only read.missing
Searching for the first index where a condition becomes true is the pattern shown in