Since the array is sorted and strictly increasing, any missing numbers must fall in the gaps between consecutive elements. For each pair of adjacent elements, we can calculate how many numbers are missing by looking at the difference minus one. As we scan through the array, we count missing numbers until we accumulate at least k of them. Once we find the gap that contains the k-th missing number, we can compute its exact value.
1 to n - 1.missedInGap = nums[i] - nums[i - 1] - 1.missedInGap >= k, the k-th missing number lies in this gap. Return nums[i - 1] + k.missedInGap from k and continue.k-th missing number is beyond the last element. Return nums[n - 1] + k.Where is the length of the input array
nums.
Instead of scanning linearly, we can use binary search to find the position where the k-th missing number falls. The key observation is that for any index i, the count of missing numbers up to that point equals nums[i] - nums[0] - i. This formula works because in a complete sequence starting from nums[0], we would expect nums[0] + i at index i. The difference tells us how many numbers were skipped. Since this count is monotonically increasing, binary search applies naturally.
left = 0 and right = n - 1.left < right:mid using upper mid to avoid infinite loops.mid: missing = nums[mid] - nums[0] - mid.missing < k, the k-th missing number is to the right, so set left = mid.right = mid - 1.left points to the largest index where the count of missing numbers is less than k.nums[0] + k + left.Where is the length of the input array
nums.
The formula for counting missing elements up to index i is nums[i] - nums[0] - i. Using nums[i] - i without subtracting nums[0] gives incorrect counts when the array does not start at 0 or 1.
When k exceeds the total count of missing elements within the array, the answer lies beyond the last element. Failing to account for this case and only searching within array bounds leads to incorrect results or out-of-bounds errors.
Using the wrong mid calculation or boundary updates can cause infinite loops or skip valid positions. The upper-mid formula mid = right - (right - left) / 2 is used here to avoid getting stuck; using standard lower-mid without proper boundary handling leads to incorrect convergence.