Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an array arr of positive integers sorted in a strictly increasing order, and an integer k.
Return the kth positive integer that is missing from this array.
Example 1:
Input: arr = [2,3,4,7,11], k = 5 Output: 9 Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,...]. The 5th missing positive integer is 9.
Example 2:
Input: arr = [1,2,3,4], k = 2 Output: 6 Explanation: The missing positive integers are [5,6,7,...]. The 2nd missing positive integer is 6.
Constraints:
1 <= arr.length <= 10001 <= arr[i] <= 10001 <= k <= 1000arr[i] < arr[j] for 1 <= i < j <= arr.lengthFollow up:
Could you solve this problem in less than O(n) complexity?
Problem summary: Given an array arr of positive integers sorted in a strictly increasing order, and an integer k. Return the kth positive integer that is missing from this array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search
[2,3,4,7,11] 5
[1,2,3,4] 2
append-k-integers-with-minimal-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1539: Kth Missing Positive Number
class Solution {
public int findKthPositive(int[] arr, int k) {
if (arr[0] > k) {
return k;
}
int left = 0, right = arr.length;
while (left < right) {
int mid = (left + right) >> 1;
if (arr[mid] - mid - 1 >= k) {
right = mid;
} else {
left = mid + 1;
}
}
return arr[left - 1] + k - (arr[left - 1] - (left - 1) - 1);
}
}
// Accepted solution for LeetCode #1539: Kth Missing Positive Number
func findKthPositive(arr []int, k int) int {
if arr[0] > k {
return k
}
left, right := 0, len(arr)
for left < right {
mid := (left + right) >> 1
if arr[mid]-mid-1 >= k {
right = mid
} else {
left = mid + 1
}
}
return arr[left-1] + k - (arr[left-1] - (left - 1) - 1)
}
# Accepted solution for LeetCode #1539: Kth Missing Positive Number
class Solution:
def findKthPositive(self, arr: List[int], k: int) -> int:
if arr[0] > k:
return k
left, right = 0, len(arr)
while left < right:
mid = (left + right) >> 1
if arr[mid] - mid - 1 >= k:
right = mid
else:
left = mid + 1
return arr[left - 1] + k - (arr[left - 1] - (left - 1) - 1)
// Accepted solution for LeetCode #1539: Kth Missing Positive Number
struct Solution;
impl Solution {
fn find_kth_positive(arr: Vec<i32>, mut k: i32) -> i32 {
let mut x = 1;
let mut i = 0;
let n = arr.len();
loop {
if i < n && x == arr[i] {
i += 1;
} else {
k -= 1;
}
if k == 0 {
break;
}
x += 1;
}
x
}
}
#[test]
fn test() {
let arr = vec![2, 3, 4, 7, 11];
let k = 5;
let res = 9;
assert_eq!(Solution::find_kth_positive(arr, k), res);
let arr = vec![1, 2, 3, 4];
let k = 2;
let res = 6;
assert_eq!(Solution::find_kth_positive(arr, k), res);
}
// Accepted solution for LeetCode #1539: Kth Missing Positive Number
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1539: Kth Missing Positive Number
// class Solution {
// public int findKthPositive(int[] arr, int k) {
// if (arr[0] > k) {
// return k;
// }
// int left = 0, right = arr.length;
// while (left < right) {
// int mid = (left + right) >> 1;
// if (arr[mid] - mid - 1 >= k) {
// right = mid;
// } else {
// left = mid + 1;
// }
// }
// return arr[left - 1] + k - (arr[left - 1] - (left - 1) - 1);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.