# Binary Search The paradigm: - Having `left` and `right` indices - Evaluate `mid` - Based on the evaluation, update `left` or `right` accordingly - When `left < right` condition is not met, return ## Segmenting ribbons - An array `a`, each element `a[i]` represents the length of a ribbon. - Obtain `k` ribbons of the same length, by cutting the ribbons. - Calculate maximum integer length`L` that is possible to obtain `k` ribbons. ```python lo, hi = 1, sum(a) // k while lo <= hi: mid = (lo + hi) // 2 possible = sum([r // mid for r in a]) # Updating lo and hi return lo - 1 ``` There are two ways to update `lo` and `hi`. ```python # First if possible: lo += 1 else: hi -= 1 # More efficient if possible: lo = mid + 1 else : hi = mid - 1 ```