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)
| 27 | // Time Complexity: O(logn) |
| 28 | // Auxiliary Space: O(1) |
| 29 | func 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 | } |
no outgoing calls
no test coverage detected