Maximum element in a sorted and rotated array

27 May, 2022
Given a sorted array arr[] of distinct elements which is rotated at some unknown point, the task is to find the maximum element in it.Examples:  
Input: arr[] = {3, 4, 5, 1, 2} Output: 5Input: arr[] = {1, 2, 3} Output: 3  
 
Approach: A simple solution is to traverse the complete array and find maximum. This solution requires O(n) time. We can do it in O(Logn) using Binary Search. If we take a closer look at above examples, we can easily figure out the following pattern:  
The maximum element is the only element whose next is smaller than it. If there is no next smaller element, then there is no rotation (last element is the maximum). We check this condition for middle element by comparing it with elements at mid – 1 and mid + 1.
If maximum element is not at middle (neither mid nor mid + 1), then maximum element lies in either left half or right half. If middle element is greater than the last element, then the maximum element lies in the left half.Else maximum element lies in the right half.
If middle element is greater than the last element, then the maximum element lies in the left half.Else maximum element lies in the right half.
If middle element is greater than the last element, then the maximum element lies in the left half.
Else maximum element lies in the right half.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
#include <bits/stdc++.h>using namespace std; // Function to return the maximum elementint findMax(int arr[], int low, int high){          if (high == low)        return arr[low];     // Find mid    int mid = low + (high - low) / 2;  // Check if mid reaches 0 ,it is greater than next element or not     if(mid==0 && arr[mid]>arr[mid+1])       {               return arr[mid];       }     // Check if mid itself is maximum element    if (mid < high && arr[mid + 1] < arr[mid] && mid>0 && arr[mid]>arr[mid-1]) {        return arr[mid];    }     // Decide whether we need to go to    // the left half or the right half    if (arr[low] > arr[mid]) {        return findMax(arr, low, mid - 1);    }    else {        return findMax(arr, mid + 1, high);    }} // Driver codeint main(){    int arr[] = { 6,5,4,3,2,1};    int n = sizeof(arr) / sizeof(arr[0]);    cout << findMax(arr, 0, n - 1);     return 0;}
// Java implementation of the approachclass GFG{     // Function to return the maximum elementstatic int findMax(int arr[], int low, int high){      // If there is only one element left    if (high == low)        return arr[low];     // Find mid    int mid = low + (high - low) / 2;  // Check if mid reaches 0 ,it is greater than next element or not  if(mid==0 && arr[mid]>arr[mid+1])  {    return arr[mid];  }      // Decide whether we need to go to    // the left half or the right half    if (arr[low] > arr[mid])    {        return findMax(arr, low, mid - 1);    }    else    {        return findMax(arr, mid + 1, high);    }} // Driver codepublic static void main(String[] args){    int arr[] = { 6,5,4,3,2,1 };    int n = arr.length;    System.out.println(findMax(arr, 0, n - 1));}}
# Python3 implementation of the approach # Function to return the maximum elementdef findMax(arr, low, high):     # If there is only one element left    if (high == low):        return arr[low]     # Find mid    mid = low + (high - low) // 2    # Check if mid reaches 0 ,it is greater than next element or not    if(mid==0 and arr[mid]>arr[mid+1]):          return arr[mid]     # Check if mid itself is maximum element    if (mid < high and arr[mid + 1] < arr[mid] and mid>0 and arr[mid]>arr[mid-1]):        return arr[mid]             # Decide whether we need to go to    # the left half or the right half    if (arr[low] > arr[mid]):        return findMax(arr, low, mid - 1)    else:        return findMax(arr, mid + 1, high) # Driver codearr = [6,5,4,3,2,1]n = len(arr)print(findMax(arr, 0, n - 1))
// C# implementation of the approachusing System; class GFG{     // Function to return the maximum elementstatic int findMax(int []arr,                   int low, int high){         // If there is only one element left    if (high == low)        return arr[low];            // Find mid    int mid = low + (high - low) / 2;    // Check if mid reaches 0 ,it is greater than next element or not    if(mid==0 && arr[mid]>arr[mid+1])        return arr[mid];     // Check if mid itself is maximum element    if (mid < high && arr[mid + 1] < arr[mid] && mid>0 && arr[mid]>arr[mid-1])    {        return arr[mid];    }     // Decide whether we need to go to    // the left half or the right half    if (arr[low] > arr[mid])    {        return findMax(arr, low, mid - 1);    }    else    {        return findMax(arr, mid + 1, high);    }} // Driver codepublic static void Main(){    int []arr = { 6,5, 1, 2, 3, 4 };    int n = arr.Length;         Console.WriteLine(findMax(arr, 0, n - 1));}}
<?php// PHP implementation of the approach // Function to return the maximum elementfunction findMax($arr, $low, $high){     // This condition is for the case when    // array is not rotated at all    if ($high <= $low)        return $arr[$low];          // Find mid    $mid = $low + ($high - $low) / 2;   // Check if mid reaches 0 ,it is greater than next element or not    if ($mid==0 && $arr[$mid]>$arr[$mid-1])          return $arr[0];     // Check if mid itself is maximum element    if ($mid < $high && $arr[$mid + 1] < $arr[$mid] && $mid > 0 && $arr[$mid]>$arr[$mid-1])    {        return $arr[$mid];    }    // Decide whether we need to go to    // the left half or the right half    if ($arr[$low] > $arr[$mid])    {        return findMax($arr, $low, $mid - 1);    }    else    {        return findMax($arr, $mid + 1, $high);    }} // Driver code$arr = array(5,6,1,2,3,4);$n = sizeof($arr);echo findMax($arr, 0, $n - 1);
<script>// Java script implementation of the approach // Function to return the maximum elementfunction findMax(arr,low,high){    // If there is only one element left    if (high == low)        return arr[low];          // Find mid    let mid = low + (high - low) / 2;    // Check if mid reaches 0 ,it is greater than next element or not    if(mid==0 && arr[mid]>arr[mid+1])    {        return arr[mid];    }     // Check if mid itself is maximum element    if (mid < high && arr[mid + 1] < arr[mid] && mid>0 && arr[mid]>arr[mid-1])    {        return arr[mid];    }     // Decide whether we need to go to    // the left half or the right half    if (arr[low] > arr[mid])    {        return findMax(arr, low, mid - 1);    }    else    {        return findMax(arr, mid + 1, high);    }} // Driver code     let arr = [ 5, 6, 1, 2, 3, 4 ];    let n = arr.length;    document.write(findMax(arr, 0, n-1 ));</script>
6
 
Time Complexity: O(logn), where n represents the size of the given array.Auxiliary Space: O(logn) due to recursive stack space.
mohit kumar 29
Code_Mech
ankthon
Akanksha_Rai
shubham7yadav
sravankumar8128
dhairyapasrija
samim2000
Binary Search
Arrays
C++
Divide and Conquer
Searching
Arrays
Searching
Divide and Conquer
Binary Search
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.