This is an interview question
There is an array of integers. The elements in the array can follow the following patterns.
- numbers are in ascending order
- numbers are in descending order
- numbers increases in the beginning and decreases in the end
- numbers decreases in the beginning and increases in the end
What is the efficient way to find the max number in the array?
In that case, all you need to do is to determine whether it’s (3). If not, the answer is max(first, last).
In the case that all elements are equal, you’ll need to exhaustively search the array to show that there’s not one high number somewhere in the middle. So I think it’s O(n) to determine whether you’re in (3).