MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / lower_bound

Function lower_bound

searches/binary_search.py:274–291  ·  view source on GitHub ↗

Returns the index of the first element greater than or equal to the item. :param sorted_collection: The sorted list to search. :param item: The item to find the lower bound for. :return: The index where the item can be inserted while maintaining order.

(sorted_collection: list[int], item: int)

Source from the content-addressed store, hash-verified

272 raise ValueError("sorted_collection must be sorted in ascending order")
273
274 def lower_bound(sorted_collection: list[int], item: int) -> int:
275 """
276 Returns the index of the first element greater than or equal to the item.
277
278 :param sorted_collection: The sorted list to search.
279 :param item: The item to find the lower bound for.
280 :return: The index where the item can be inserted while maintaining order.
281 """
282 left = 0
283 right = len(sorted_collection)
284 while left < right:
285 midpoint = left + (right - left) // 2
286 current_item = sorted_collection[midpoint]
287 if current_item < item:
288 left = midpoint + 1
289 else:
290 right = midpoint
291 return left
292
293 def upper_bound(sorted_collection: list[int], item: int) -> int:
294 """

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected