DEV Community

Abhishek Gupta
Abhishek Gupta

Posted on

๐Ÿง  Binary Search: 0 Monster Guide (JavaScript, Simple Words)

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;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  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
Enter fullscreen mode Exit fullscreen mode
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;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ 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;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ 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;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Š 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;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” 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
  }
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ 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
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  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;
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ฅ 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
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ 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:

  1. Find element
  2. First & Last position
  3. Count occurrences
  4. Search in rotated array
  5. Peak element
  6. Koko Eating Bananas
  7. Aggressive Cows

Top comments (0)