Binary-search

70 articles

dsa3 min read

Find K Closest Elements

Find K closest elements to x in a sorted array using binary search to find the optimal left boundary.

Read →
dsa3 min read

Swim in Rising Water

Find minimum time to swim from top-left to bottom-right in a grid where time = max elevation on the path.

Read →
dsa2 min read

Ugly Number III

Find the nth number divisible by a, b, or c using binary search with inclusion-exclusion counting formula.

Read →
dsa1 min read

Find Right Interval

For each interval find the interval with the smallest start point >= the end point using sorted starts and binary search.

Read →
dsa2 min read

Count Complete Tree Nodes

Count nodes in a complete binary tree in O(log^2 n) by comparing left and right heights to identify full subtrees.

Read →