Modified to accomplish the task in less than O(n) complexity, using a binary search algorithm.