Missing Element in Sorted Array
Description
null
Solution(javascript)
/** 1: Binary Search
* 求最大的index, 使 missingCount < k
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
const missingElement = function (nums, k) {
const missingCount = (left, right) => nums[right] - nums[left] - (right - left)
let left = 0
let right = nums.length - 1
let result = -1
while (left <= right) {
const middle = Math.floor(left + (right - left) / 2)
const count = missingCount(0, middle)
if (count < k) {
left = middle + 1
result = middle
} else {
right = middle - 1
}
}
return k - missingCount(0, result) + nums[result]
}