Episode 7 — DSA with JavaScript / 7.10 — Binary Search

7.10.a — Binary Search Fundamentals

<< 7.10 Overview


What is binary search?

Binary search finds a target in a sorted array by halving the search space.

Array: [2, 5, 8, 12, 16, 23, 38, 56, 72, 91]
Target: 23

Step 1: lo=0, hi=9, mid=4 → arr[4]=16 < 23 → lo=5
Step 2: lo=5, hi=9, mid=7 → arr[7]=56 > 23 → hi=6
Step 3: lo=5, hi=6, mid=5 → arr[5]=23 ✓ found!

Only 3 comparisons instead of 10 (linear search)!
Search space narrowing:
[2, 5, 8, 12, 16, 23, 38, 56, 72, 91]   ← full array
                 [23, 38, 56, 72, 91]   ← right half
                 [23, 38]               ← left of mid
                 [23]                   ← found!

JavaScript

function binarySearch(arr, target) {
    let lo = 0, hi = arr.length - 1;
    while (lo <= hi) {
        const mid = lo + Math.floor((hi - lo) / 2);
        if (arr[mid] === target) return mid;
        if (arr[mid] < target) lo = mid + 1;
        else hi = mid - 1;
    }
    return -1;
}

C++

int binarySearch(const vector<int>& arr, int target) {
    int lo = 0, hi = arr.size() - 1;
    while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        if (arr[mid] == target) return mid;
        if (arr[mid] < target) lo = mid + 1;
        else hi = mid - 1;
    }
    return -1;
}

Time: O(log n) | Space: O(1)


Variations

Find first occurrence (lower bound)

function lowerBound(arr, target) {
    let lo = 0, hi = arr.length - 1, result = -1;
    while (lo <= hi) {
        const mid = lo + Math.floor((hi - lo) / 2);
        if (arr[mid] >= target) { result = mid; hi = mid - 1; }
        else lo = mid + 1;
    }
    return result !== -1 && arr[result] === target ? result : -1;
}

Find last occurrence (upper bound)

function upperBound(arr, target) {
    let lo = 0, hi = arr.length - 1, result = -1;
    while (lo <= hi) {
        const mid = lo + Math.floor((hi - lo) / 2);
        if (arr[mid] <= target) { result = mid; lo = mid + 1; }
        else hi = mid - 1;
    }
    return result !== -1 && arr[result] === target ? result : -1;
}

Search in rotated sorted array

[4, 5, 6, 7, 0, 1, 2]  target=0

mid=3 → arr[3]=7
Left half [4,5,6,7] is sorted, target not in range → search right
lo=4, hi=6, mid=5 → arr[5]=1 > 0 → hi=4
lo=4, hi=4, mid=4 → arr[4]=0 ✓
function searchRotated(arr, target) {
    let lo = 0, hi = arr.length - 1;
    while (lo <= hi) {
        const mid = lo + Math.floor((hi - lo) / 2);
        if (arr[mid] === target) return mid;
        if (arr[lo] <= arr[mid]) {
            if (arr[lo] <= target && target < arr[mid]) hi = mid - 1;
            else lo = mid + 1;
        } else {
            if (arr[mid] < target && target <= arr[hi]) lo = mid + 1;
            else hi = mid - 1;
        }
    }
    return -1;
}
int searchRotated(vector<int>& arr, int target) {
    int lo = 0, hi = arr.size() - 1;
    while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        if (arr[mid] == target) return mid;
        if (arr[lo] <= arr[mid]) {
            if (arr[lo] <= target && target < arr[mid]) hi = mid - 1;
            else lo = mid + 1;
        } else {
            if (arr[mid] < target && target <= arr[hi]) lo = mid + 1;
            else hi = mid - 1;
        }
    }
    return -1;
}

Search on infinite array

function searchInfinite(arr, target) {
    let lo = 0, hi = 1;
    while (arr[hi] < target) { lo = hi; hi *= 2; }
    return binarySearch(arr, target, lo, hi);
}

Search in 2D matrix

function searchMatrix(matrix, target) {
    const m = matrix.length, n = matrix[0].length;
    let lo = 0, hi = m * n - 1;
    while (lo <= hi) {
        const mid = Math.floor((lo + hi) / 2);
        const val = matrix[Math.floor(mid / n)][mid % n];
        if (val === target) return true;
        val < target ? lo = mid + 1 : hi = mid - 1;
    }
    return false;
}

Find peak element

function findPeak(arr) {
    let lo = 0, hi = arr.length - 1;
    while (lo < hi) {
        const mid = lo + Math.floor((hi - lo) / 2);
        if (arr[mid] < arr[mid + 1]) lo = mid + 1;
        else hi = mid;
    }
    return lo;
}

Find minimum in rotated array

function findMin(arr) {
    let lo = 0, hi = arr.length - 1;
    while (lo < hi) {
        const mid = lo + Math.floor((hi - lo) / 2);
        if (arr[mid] > arr[hi]) lo = mid + 1;
        else hi = mid;
    }
    return arr[lo];
}

Binary search on answer (parametric)

// Find minimum eating speed to finish bananas in H hours
function minEatingSpeed(piles, H) {
    let lo = 1, hi = Math.max(...piles);
    while (lo < hi) {
        const mid = Math.floor((lo + hi) / 2);
        const hours = piles.reduce((s, p) => s + Math.ceil(p / mid), 0);
        if (hours <= H) hi = mid;
        else lo = mid + 1;
    }
    return lo;
}

Real-world use cases

  1. Dictionary lookup — find a word in sorted word list
  2. Git bisect — find the commit that introduced a bug
  3. Database indexing — B-tree search
  4. IP routing — longest prefix match
  5. Version control — find when a test started failing

Scenario bank (binary search)

7.10a-001 — Find square root using binary search (v1)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-002 — Find first bad version (v2)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-003 — Search insert position (v3)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-004 — Count occurrences of target (v4)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-005 — Find smallest letter greater than target (v5)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-006 — Find kth missing positive number (v6)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-007 — Capacity to ship packages in D days (v7)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-008 — Split array largest sum (v8)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-009 — Median of two sorted arrays (v9)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-010 — Aggressive cows / magnetic balls (v10)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-011 — Allocate minimum pages (v11)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-012 — Find peak in mountain array (v12)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-013 — Search in bitonic array (v13)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-014 — Minimize maximum distance to gas station (v14)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-015 — Find nth magical number (v15)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-016 — Binary search on floating point (v16)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-017 — Find rotation count in rotated array (v17)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-018 — Search in nearly sorted array (v18)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-019 — Find pair with given difference (v19)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-020 — Binary search with unknown array size (v20)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-021 — Find square root using binary search (v21)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-022 — Find first bad version (v22)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-023 — Search insert position (v23)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-024 — Count occurrences of target (v24)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-025 — Find smallest letter greater than target (v25)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-026 — Find kth missing positive number (v26)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-027 — Capacity to ship packages in D days (v27)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-028 — Split array largest sum (v28)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-029 — Median of two sorted arrays (v29)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-030 — Aggressive cows / magnetic balls (v30)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-031 — Allocate minimum pages (v31)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-032 — Find peak in mountain array (v32)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-033 — Search in bitonic array (v33)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-034 — Minimize maximum distance to gas station (v34)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-035 — Find nth magical number (v35)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-036 — Binary search on floating point (v36)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-037 — Find rotation count in rotated array (v37)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-038 — Search in nearly sorted array (v38)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-039 — Find pair with given difference (v39)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-040 — Binary search with unknown array size (v40)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-041 — Find square root using binary search (v41)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-042 — Find first bad version (v42)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-043 — Search insert position (v43)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-044 — Count occurrences of target (v44)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-045 — Find smallest letter greater than target (v45)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-046 — Find kth missing positive number (v46)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-047 — Capacity to ship packages in D days (v47)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-048 — Split array largest sum (v48)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-049 — Median of two sorted arrays (v49)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-050 — Aggressive cows / magnetic balls (v50)

  • Level: Beginner
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-051 — Allocate minimum pages (v51)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-052 — Find peak in mountain array (v52)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-053 — Search in bitonic array (v53)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-054 — Minimize maximum distance to gas station (v54)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-055 — Find nth magical number (v55)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-056 — Binary search on floating point (v56)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-057 — Find rotation count in rotated array (v57)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-058 — Search in nearly sorted array (v58)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-059 — Find pair with given difference (v59)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-060 — Binary search with unknown array size (v60)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-061 — Find square root using binary search (v61)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-062 — Find first bad version (v62)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-063 — Search insert position (v63)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-064 — Count occurrences of target (v64)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-065 — Find smallest letter greater than target (v65)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-066 — Find kth missing positive number (v66)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-067 — Capacity to ship packages in D days (v67)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-068 — Split array largest sum (v68)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-069 — Median of two sorted arrays (v69)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-070 — Aggressive cows / magnetic balls (v70)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-071 — Allocate minimum pages (v71)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-072 — Find peak in mountain array (v72)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-073 — Search in bitonic array (v73)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-074 — Minimize maximum distance to gas station (v74)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-075 — Find nth magical number (v75)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-076 — Binary search on floating point (v76)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-077 — Find rotation count in rotated array (v77)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-078 — Search in nearly sorted array (v78)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-079 — Find pair with given difference (v79)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-080 — Binary search with unknown array size (v80)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-081 — Find square root using binary search (v81)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-082 — Find first bad version (v82)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-083 — Search insert position (v83)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-084 — Count occurrences of target (v84)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-085 — Find smallest letter greater than target (v85)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-086 — Find kth missing positive number (v86)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-087 — Capacity to ship packages in D days (v87)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-088 — Split array largest sum (v88)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-089 — Median of two sorted arrays (v89)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-090 — Aggressive cows / magnetic balls (v90)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-091 — Allocate minimum pages (v91)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-092 — Find peak in mountain array (v92)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-093 — Search in bitonic array (v93)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-094 — Minimize maximum distance to gas station (v94)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-095 — Find nth magical number (v95)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-096 — Binary search on floating point (v96)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-097 — Find rotation count in rotated array (v97)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-098 — Search in nearly sorted array (v98)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-099 — Find pair with given difference (v99)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-100 — Binary search with unknown array size (v100)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-101 — Find square root using binary search (v101)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-102 — Find first bad version (v102)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-103 — Search insert position (v103)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-104 — Count occurrences of target (v104)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-105 — Find smallest letter greater than target (v105)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-106 — Find kth missing positive number (v106)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-107 — Capacity to ship packages in D days (v107)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-108 — Split array largest sum (v108)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-109 — Median of two sorted arrays (v109)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-110 — Aggressive cows / magnetic balls (v110)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-111 — Allocate minimum pages (v111)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-112 — Find peak in mountain array (v112)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-113 — Search in bitonic array (v113)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-114 — Minimize maximum distance to gas station (v114)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-115 — Find nth magical number (v115)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-116 — Binary search on floating point (v116)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-117 — Find rotation count in rotated array (v117)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-118 — Search in nearly sorted array (v118)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-119 — Find pair with given difference (v119)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-120 — Binary search with unknown array size (v120)

  • Level: Intermediate
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-121 — Find square root using binary search (v121)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-122 — Find first bad version (v122)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-123 — Search insert position (v123)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-124 — Count occurrences of target (v124)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-125 — Find smallest letter greater than target (v125)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-126 — Find kth missing positive number (v126)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-127 — Capacity to ship packages in D days (v127)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-128 — Split array largest sum (v128)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-129 — Median of two sorted arrays (v129)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-130 — Aggressive cows / magnetic balls (v130)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-131 — Allocate minimum pages (v131)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-132 — Find peak in mountain array (v132)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-133 — Search in bitonic array (v133)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-134 — Minimize maximum distance to gas station (v134)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-135 — Find nth magical number (v135)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-136 — Binary search on floating point (v136)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-137 — Find rotation count in rotated array (v137)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-138 — Search in nearly sorted array (v138)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-139 — Find pair with given difference (v139)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-140 — Binary search with unknown array size (v140)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-141 — Find square root using binary search (v141)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-142 — Find first bad version (v142)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-143 — Search insert position (v143)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-144 — Count occurrences of target (v144)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-145 — Find smallest letter greater than target (v145)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-146 — Find kth missing positive number (v146)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-147 — Capacity to ship packages in D days (v147)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-148 — Split array largest sum (v148)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-149 — Median of two sorted arrays (v149)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-150 — Aggressive cows / magnetic balls (v150)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-151 — Allocate minimum pages (v151)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-152 — Find peak in mountain array (v152)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-153 — Search in bitonic array (v153)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-154 — Minimize maximum distance to gas station (v154)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-155 — Find nth magical number (v155)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-156 — Binary search on floating point (v156)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-157 — Find rotation count in rotated array (v157)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-158 — Search in nearly sorted array (v158)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-159 — Find pair with given difference (v159)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-160 — Binary search with unknown array size (v160)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-161 — Find square root using binary search (v161)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-162 — Find first bad version (v162)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-163 — Search insert position (v163)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-164 — Count occurrences of target (v164)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-165 — Find smallest letter greater than target (v165)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-166 — Find kth missing positive number (v166)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-167 — Capacity to ship packages in D days (v167)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-168 — Split array largest sum (v168)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-169 — Median of two sorted arrays (v169)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.

7.10a-170 — Aggressive cows / magnetic balls (v170)

  • Level: Advanced
  • Approach: Apply technique.
  • Time/Space: Analyze.
  • Edge cases: empty, single, boundary.
  • JS & C++: Both.