If binary search ever felt confusing, itโs not because itโs hardโitโs because itโs taught in pieces.
This guide will take you from:
๐ absolute beginner
๐ to solving advanced interview problems
No fluff. Just clear thinking + simple JS code.
๐ 0. What You MUST Know First
๐ Binary search ONLY works on sorted arrays
If the array is not sorted โ โ donโt use binary search
๐ 1. Core Idea (Super Simple)
Instead of checking every number:
๐ Go to the middle
๐ Decide:
- go LEFT
- go RIGHT
Repeat.
๐งฉ 2. Basic Binary Search
๐ Find if a number exists
function binarySearch(arr, target) {
let low = 0, high = arr.length - 1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) return mid;
if (arr[mid] < target) {
low = mid + 1; // go right
} else {
high = mid - 1; // go left
}
}
return -1;
}
๐ง 3. The ONLY Thing You Need to Master
๐ Direction decision:
-
arr[mid] < targetโ go RIGHT -
arr[mid] > targetโ go LEFT
Everything else = variation of this
๐ 4. First Occurrence (Leftmost)
๐ When duplicates exist
Example:
[1, 2, 2, 2, 3]
โ want THIS one
function firstOccurrence(arr, target) {
let low = 0, high = arr.length - 1;
let ans = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) {
ans = mid;
high = mid - 1; // go left again
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return ans;
}
๐ Rule: found โ still go LEFT
๐ 5. Last Occurrence (Rightmost)
function lastOccurrence(arr, target) {
let low = 0, high = arr.length - 1;
let ans = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === target) {
ans = mid;
low = mid + 1; // go right again
} else if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return ans;
}
๐ Rule: found โ still go RIGHT
๐ 6. Lower Bound (first โฅ target)
๐ First number โฅ target
function lowerBound(arr, target) {
let low = 0, high = arr.length;
while (low < high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] < target) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
๐ 7. Upper Bound (first > target)
๐ First number > target
function upperBound(arr, target) {
let low = 0, high = arr.length;
while (low < high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] <= target) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
๐ 8. One Template to Rule Them All
๐ Instead of memorizing everything:
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (condition) {
high = mid - 1; // go left
} else {
low = mid + 1; // go right
}
}
๐ Just change the condition
๐ฅ 9. Binary Search on Answer (GAME CHANGER)
๐ Not searching array
๐ Searching answer range
๐ก Example Thinking
Question:
๐ Minimum speed to finish work?
Search space:
low = 1
high = max possible
๐ง Pattern:
function searchAnswer(low, high) {
let ans = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (isPossible(mid)) {
ans = mid;
high = mid - 1; // try better
} else {
low = mid + 1;
}
}
return ans;
}
๐ฅ 10. REAL SECRET (Most Important Insight)
Binary search is NOT about arrays.
๐ It is about finding a boundary
Example:
F F F F T T T
โ answer
๐ Find first TRUE
โ ๏ธ 11. Common Mistakes
โ Using on unsorted array
โ Infinite loop (wrong condition)
โ Forgetting edge cases
โ Confusing < vs <=
๐งช 12. Practice Problems (Must Do)
Start easy โ go hard:
- Find element
- First & Last position
- Count occurrences
- Search in rotated array
- Peak element
- Koko Eating Bananas
- Aggressive Cows
Top comments (0)