This solution is generated by ChatGPT, correctness is not guaranteed. You might also be able to find a solution fromcommunity posts.
AI solution for JavaScript Coding Question on BFE.dev
49. search last index with Binary Search(possible duplicate array)
/** * @param {number[]} arr - ascending order array with possible duplicates * @param {number} target * @returns {number} - last index of target number in arr, -1 if not found */function binarySearchLast(arr, target) { // your code here}
Example:
const arr = [1,2,2,2,3,3,4,5,6];console.log(binarySearchLast(arr, 2)); // 3console.log(binarySearchLast(arr, 3)); // 5console.log(binarySearchLast(arr, 6)); // 8console.log(binarySearchLast(arr, 0)); // -1