tags:


1. Binary Search and related #

Created Mon Jul 29, 2024 at 9:48 PM

The mother of all divide-and-conquer algorithms is binary search.

Counting occurrences #

A complete logn solution is possible. We need to find left and right index of the block of keys (they will be continuous), i.e. use lower-bound and upper-bound.

Proof that lower and upper bound binary search is possible - lets prove only upper. Since we disregard one side (left or right) in binary search, we can keep disregarding values > key (but note it) as well as <= key. This way the upper bound will keep on improving (i.e. moving left).

Two binary searches are still logarithmic. So logn.

Suppose we have an array that has 0s at the beginning, and then an unbouned number of 1s afterwards.

Usual binary search (start + end/2) won’t work here, since 1s are unbounded.

But binary search still applies here. Instead of partitioning the space, like usual, we need to start from some point (0 index is fine), and do jumps. And the place a jump lands at will decide which direction to jump next. We of course jump twice the distance as much as the previous jump.

This is called one sided binary search.

Square root and solving equations #

The ‘bisection method’ is a technique in numerical analysis based on binary search.

Binary search and its variants are the quintessential divide-and-conquer algorithms.