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.
[Naive Approach] Linear Search - O(n) Time and O(1) Space
A simple solution is to use linear search to traverse the complete array and find a maximum.
#include <iostream>
#include <vector>
using namespace std;
int findMax(vector<int>& arr) {
int res = arr[0];
// Traverse over arr[] to find maximum element
for (int i = 1; i < arr.size(); i++)
res = max(res, arr[i]);
return res;
}
int main() {
vector<int> arr = {5, 6, 1, 2, 3, 4};
cout << findMax(arr) << endl;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int findMax(int arr[], int size) {
int res = arr[0];
// Traverse over arr[] to find maximum element
for (int i = 1; i < size; i++)
res = (res > arr[i]) ? res : arr[i];
return res;
}
int main() {
int arr[] = {5, 6, 1, 2, 3, 4};
int size = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", findMax(arr, size));
return 0;
}
import java.util.Arrays;
public class GfG {
public static int findMax(int[] arr) {
int res = arr[0];
// Traverse over arr[] to find maximum element
for (int i = 1; i < arr.length; i++)
res = Math.max(res, arr[i]);
return res;
}
public static void main(String[] args) {
int[] arr = {5, 6, 1, 2, 3, 4};
System.out.println(findMax(arr));
}
}
def findMax(arr):
res = arr[0]
# Traverse over arr[] to find maximum element
for i in range(1, len(arr)):
res = max(res, arr[i])
return res
if __name__ == '__main__':
arr = [5, 6, 1, 2, 3, 4]
print(findMax(arr))
using System;
class GfG
{
static int findMax(int[] arr)
{
int res = arr[0];
// Traverse over arr[] to find maximum element
for (int i = 1; i < arr.Length; i++)
res = Math.Max(res, arr[i]);
return res;
}
static void Main()
{
int[] arr = { 5, 6, 1, 2, 3, 4 };
Console.WriteLine(findMax(arr));
}
}
function findMax(arr) {
let res = arr[0];
// Traverse over arr to find maximum element
for (let i = 1; i < arr.length; i++)
res = Math.max(res, arr[i]);
return res;
}
const arr = [5, 6, 1, 2, 3, 4];
console.log(findMax(arr));
Output
6
[Expected Approach] Binary Search - O(log n) Time and O(1) Space
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.
#include <iostream>
#include <vector>
using namespace std;
int findMax(vector<int> &arr)
{
int low = 0, high = arr.size() - 1;
while (low < high)
{
// If current range is sorted, last element is the maximum
if (arr[low] < arr[high])
return arr[high];
int mid = low + (high - low) / 2;
// Left half is sorted, maximum lies at mid or to the right
if (arr[mid] > arr[low])
low = mid;
// Maximum lies in left half
else
high = mid - 1;
}
// low == high points to the maximum element
return arr[low];
}
int main()
{
vector<int> arr = {7, 8, 9, 10, 1, 2, 3, 4, 5};
cout << findMax(arr);
return 0;
}
#include <stdio.h>
int findMax(int arr[], int n)
{
int low = 0, high = n - 1;
while (low < high)
{
// If current range is sorted, last element is the maximum
if (arr[low] <= arr[high])
return arr[high];
int mid = low + (high - low) / 2;
// Left half is sorted, maximum lies at mid or to the right
if (arr[mid] > arr[low])
low = mid;
// Maximum lies in left half
else
high = mid - 1;
}
// low == high points to the maximum element
return arr[low];
}
int main()
{
int arr[] = {7, 8, 9, 10, 1, 2, 3, 4, 5};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", findMax(arr, n));
return 0;
}
class GfG {
static int findMax(int[] arr)
{
int low = 0, high = arr.length - 1;
while (low < high)
{
// If current range is sorted, last element is the maximum
if (arr[low] <= arr[high])
return arr[high];
int mid = low + (high - low) / 2;
// Left half is sorted, maximum lies at mid or to the right
if (arr[mid] > arr[low])
low = mid;
// Maximum lies in left half
else
high = mid - 1;
}
// low == high points to the maximum element
return arr[low];
}
public static void main(String[] args)
{
int[] arr = {7, 8, 9, 10, 1, 2, 3, 4, 5};
System.out.print(findMax(arr));
}
}
def findMax(arr):
low, high = 0, len(arr) - 1
while low < high:
# If current range is sorted, last element is the maximum
if arr[low] <= arr[high]:
return arr[high]
mid = low + (high - low) // 2
# Left half is sorted, maximum lies at mid or to the right
if arr[mid] > arr[low]:
low = mid
# Maximum lies in left half
else:
high = mid - 1
# low == high points to the maximum element
return arr[low]
arr = [7, 8, 9, 10, 1, 2, 3, 4, 5]
print(findMax(arr))
using System;
class GfG
{
static int findMax(int[] arr)
{
int low = 0, high = arr.Length - 1;
while (low < high)
{
// If current range is sorted, last element is the maximum
if (arr[low] <= arr[high])
return arr[high];
int mid = low + (high - low) / 2;
// Left half is sorted, maximum lies at mid or to the right
if (arr[mid] > arr[low])
low = mid;
// Maximum lies in left half
else
high = mid - 1;
}
// low == high points to the maximum element
return arr[low];
}
static void Main()
{
int[] arr = { 7, 8, 9, 10, 1, 2, 3, 4, 5 };
Console.Write(findMax(arr));
}
}
function findMax(arr) {
let low = 0, high = arr.length - 1;
while (low < high) {
// If current range is sorted, last element is the maximum
if (arr[low] <= arr[high])
return arr[high];
let mid = Math.floor(low + (high - low) / 2);
// Left half is sorted, maximum lies at mid or to the right
if (arr[mid] > arr[low])
low = mid;
// Maximum lies in left half
else
high = mid - 1;
}
// low == high points to the maximum element
return arr[low];
}
const arr = [7, 8, 9, 10, 1, 2, 3, 4, 5];
console.log(findMax(arr));
Output
10