Binary Search
44 / 170Binary SearchLesson 6 of 8About 41 min
Python ships the boundary loop. Knowing which of its two functions you want is the whole skill.
The problem below is presented as a class with methods rather than a single function, which makes it look like a different kind of exercise. It is not. Decide what each method must store and what each query must read, and the searching is one bisect call.
Type the four numbers this prints, separated by single spaces.
from bisect import bisect_left, bisect_right values = [2, 4, 4, 4, 7, 9] print(bisect_left(values, 4), bisect_right(values, 4), bisect_left(values, 5), bisect_right(values, 1))Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.