Greedy and binary search in JavaScript
Practice this questionWANT TO UNDERSTAND THE CONCEPT FIRST?Learn Greedy and binary search in JavaScript
Common greedy questions
- Jump Game
- Assign Cookies
- Activity Selection
- Minimum Coins
Binary search
function binarySearch(arr, target) {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (arr[mid] === target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}Adapted from 16_DSA_JAVASCRIPT.md · “11. Greedy and binary search in JavaScript”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.