Skip to content

Commit 3638104

Browse files
authored
Update 1060-missing-element-in-sorted-array.js
1 parent bb263be commit 3638104

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

1060-missing-element-in-sorted-array.js

+24
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,27 @@ const missingElement = function(nums, k) {
7575
return nums[l] + k
7676
}
7777

78+
// another
79+
80+
/**
81+
* @param {number[]} nums
82+
* @param {number} k
83+
* @return {number}
84+
*/
85+
const missingElement = function(nums, k) {
86+
const n = nums.length
87+
if (k > missing(nums, n - 1)) return nums[n - 1] + k - missing(nums, n - 1)
88+
let left = 0,
89+
right = n - 1,
90+
pivot
91+
while (left < right) {
92+
pivot = left + Math.floor((right - left) / 2)
93+
if (missing(nums, pivot) < k) left = pivot + 1
94+
else right = pivot
95+
}
96+
return nums[left - 1] + k - missing(nums, left - 1)
97+
}
98+
function missing(arr, idx) {
99+
return arr[idx] - arr[0] - idx
100+
}
101+

0 commit comments

Comments
 (0)