Missing Element in Sorted Array - Problem
Given an integer array nums which is sorted in ascending order and all of its elements are unique, and given also an integer k, return the k-th missing number starting from the leftmost number of the array.
The missing numbers are the numbers that are not present in the array but would appear in the complete sequence starting from nums[0].
Example: If nums = [4, 7, 9, 10] and k = 1, the complete sequence starting from 4 would be [4, 5, 6, 7, 8, 9, 10, 11, ...]. The missing numbers are [5, 6, 8, 11, ...], so the 1st missing number is 5.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [4,7,9,10], k = 1
›
Output:
5
💡 Note:
The complete sequence starting from 4 is [4,5,6,7,8,9,10,11,...]. Missing numbers are [5,6,8,11,...], so the 1st missing is 5.
Example 2 — Larger K
$
Input:
nums = [4,7,9,10], k = 3
›
Output:
8
💡 Note:
Missing numbers are [5,6,8,11,...]. The 3rd missing number is 8.
Example 3 — Missing Before Array
$
Input:
nums = [1,2,4], k = 3
›
Output:
6
💡 Note:
Complete sequence: [1,2,3,4,5,6,...]. Missing: [3,5,6,...]. The 3rd missing is 6.
Constraints
- 1 ≤ nums.length ≤ 5 × 104
- -109 ≤ nums[i] ≤ 109
- 1 ≤ k ≤ 109
- nums is sorted in ascending order
Visualization
Tap to expand
Understanding the Visualization
1
Input
Sorted array [4,7,9,10] and k=1
2
Process
Identify missing numbers in complete sequence
3
Output
Return the 1st missing number: 5
Key Takeaway
🎯 Key Insight: Use the formula nums[i] - nums[0] - i to count missing numbers at any position
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code