MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / lowerBound

Function lowerBound

dynamic/longestincreasingsubsequencegreedy.go:29–43  ·  view source on GitHub ↗

Function to find the leftmost index in arr with value >= val, mimicking the inbuild lower_bound function in C++ Time Complexity: O(logn) Auxiliary Space: O(1)

(arr []int, val int)

Source from the content-addressed store, hash-verified

27// Time Complexity: O(logn)
28// Auxiliary Space: O(1)
29func lowerBound(arr []int, val int) int {
30 searchWindowLeft, searchWindowRight := 0, len(arr)-1
31
32 for searchWindowLeft <= searchWindowRight {
33 middle := (searchWindowLeft + searchWindowRight) / 2
34
35 if arr[middle] < val {
36 searchWindowLeft = middle + 1
37 } else {
38 searchWindowRight = middle - 1
39 }
40 }
41
42 return searchWindowRight + 1
43}

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected