![]() |
VOOZH | about |
Given a sorted array arr[] (may contain duplicates) of size n that is rotated at some unknown point, the task is to find the maximum element in it.
Examples:
Input: arr[] = [5, 6, 1, 2, 3, 4]
Output: 6
Explanation: 6 is the maximum element present in the array.Input: arr[] = [3, 2, 2, 2]
Output: 3
Explanation: 3 is the maximum element present in the array.
A simple solution is to use linear search to traverse the complete array and find a maximum.
6
The array is sorted and then rotated, which means it consists of two individually sorted parts.
The maximum element lies just before the rotation point, i.e., at the end of the first sorted part.Using Binary Search, we can efficiently locate this element by observing whether the current search range is already sorted. If it is sorted, the last element of that range is the maximum. Otherwise, we compare the middle element with the first element to decide which half contains the maximum and discard the other half.
10