Understanding upper bound algorithm


The strategy from the earlier example could be used to create an algorithm. To explain this algorithm, we will use an array sorted in ascending order and find the upper bound of a target number.

Algorithm

The upper-bound algorithm is used to find the first position in a sorted array where a given target value can be exceeded. In other words, it returns the index of the first element that is strictly greater than the target value. This is useful for insertion, range queries, and counting elements greater than a value. The algorithm begins by initializing two indices that define the current search range in which the upper-bound may exist.

Liking the course? Check our discounted plans to continue learning.