Intern
In the standard Go library, the function sort.Search(n, f) finds the minimal index i for which f(i) == true. What property must the results of f have over indices 0...n-1 for the search to work correctly?
sobes.tech AI
Answer from AI
The function f, passed to sort.Search, must be monotonically non-decreasing by index from 0 to n-1, meaning it should return false initially, and from some index i onwards — true for all subsequent indices. This guarantees that there exists a minimal index i for which f(i) == true, and binary search will correctly find it.
Example: if f(i) = (i >= k), then for all i < k, f(i) == false, and for i >= k — true.