Maximum element in a sorted and rotated array

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: 5



Input: 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.
    1. If middle element is greater than the last element, then the maximum element lies in the left half.
    2. Else maximum element lies in the right half.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the maximum element
int findMax(int arr[], int low, int high)
{
  
    // This condition is for the case when
    // array is not rotated at all
    if (high < low)
        return arr[0];
  
    // If there is only one element left
    if (high == low)
        return arr[low];
  
    // Find mid
    int mid = low + (high - low) / 2;
  
    // Check if mid itself is maximum element
    if (mid < high && arr[mid + 1] < arr[mid]) {
        return arr[mid];
    }
  
    // Check if element at (mid - 1) is maximum element
    // Consider the cases like {4, 5, 1, 2, 3}
    if (mid > low && arr[mid] < arr[mid - 1]) {
        return arr[mid - 1];
    }
  
    // 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
int main()
{
    int arr[] = { 5, 6, 1, 2, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findMax(arr, 0, n - 1);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
      
// Function to return the maximum element
static int findMax(int arr[], int low, int high)
{
  
    // This condition is for the case when
    // array is not rotated at all
    if (high < low)
        return arr[0];
  
    // If there is only one element left
    if (high == low)
        return arr[low];
  
    // Find mid
    int mid = low + (high - low) / 2;
  
    // Check if mid itself is maximum element
    if (mid < high && arr[mid + 1] < arr[mid])
    {
        return arr[mid];
    }
  
    // Check if element at (mid - 1) is maximum element
    // Consider the cases like {4, 5, 1, 2, 3}
    if (mid > low && arr[mid] < arr[mid - 1])
    {
        return arr[mid - 1];
    }
  
    // 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
public static void main(String[] args)
{
    int arr[] = { 5, 6, 1, 2, 3, 4 };
    int n = arr.length;
    System.out.println(findMax(arr, 0, n - 1));
}
}
  
// This code is contributed by Code_Mech.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the maximum element
def findMax(arr, low, high):
  
    # This condition is for the case when
    # array is not rotated at all
    if (high < low):
        return arr[0]
  
    # If there is only one element left
    if (high == low):
        return arr[low]
  
    # Find mid
    mid = low + (high - low) // 2
  
    # Check if mid itself is maximum element
    if (mid < high and arr[mid + 1] < arr[mid]):
        return arr[mid]
      
    # Check if element at (mid - 1) is maximum element
    # Consider the cases like {4, 5, 1, 2, 3}
    if (mid > low and arr[mid] < arr[mid - 1]):
        return arr[mid - 1]
  
    # 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 = [5, 6, 1, 2, 3, 4]
n = len(arr)
print(findMax(arr, 0, n - 1))
  
# This code is contributed by mohit kumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the maximum element
static int findMax(int []arr, 
                   int low, int high)
{
  
    // This condition is for the case 
    // when array is not rotated at all
    if (high < low)
        return arr[0];
  
    // If there is only one element left
    if (high == low)
        return arr[low];
  
    // Find mid
    int mid = low + (high - low) / 2;
  
    // Check if mid itself is maximum element
    if (mid < high && arr[mid + 1] < arr[mid])
    {
        return arr[mid];
    }
  
    // Check if element at (mid - 1) 
    // is maximum element
    // Consider the cases like {4, 5, 1, 2, 3}
    if (mid > low && arr[mid] < arr[mid - 1])
    {
        return arr[mid - 1];
    }
  
    // 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
public static void Main()
{
    int []arr = { 5, 6, 1, 2, 3, 4 };
    int n = arr.Length;
      
    Console.WriteLine(findMax(arr, 0, n - 1));
}
}
  
// This code is contributed by Ryuga

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach
  
// Function to return the maximum element
function findMax($arr, $low, $high)
{
  
    // This condition is for the case when
    // array is not rotated at all
    if ($high < $low)
        return $arr[0];
  
    // If there is only one element left
    if ($high == $low)
        return $arr[$low];
  
    // Find mid
    $mid = $low + ($high - $low) / 2;
  
    // Check if mid itself is maximum element
    if ($mid < $high && $arr[$mid + 1] < $arr[$mid]) 
    {
        return $arr[$mid];
    }
  
    // Check if element at (mid - 1) is maximum element
    // Consider the cases like {4, 5, 1, 2, 3}
    if ($mid > $low && $arr[$mid] < $arr[$mid - 1])
    {
        return $arr[$mid - 1];
    }
  
    // 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);
  
// This code is contributed
// by Akanksha Rai

chevron_right


Output:

6


My Personal Notes arrow_drop_up

Competitive Programmer, Full Stack Developer, Technical Content Writer, Machine Learner

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.