Skip to content
Related Articles

Related Articles

Improve Article
Maximum element in a sorted and rotated array
  • Difficulty Level : Easy
  • Last Updated : 06 Jun, 2021

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:
 

 

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++




#include <bits/stdc++.h>
using namespace std;
 
// Function to return the maximum element
int 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 code
int 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




// Java implementation of the approach
class GFG
{
     
// Function to return the maximum element
static 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 code
public 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




# Python3 implementation of the approach
 
# Function to return the maximum element
def 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 code
arr = [6,5,4,3,2,1]
n = len(arr)
print(findMax(arr, 0, n - 1))

C#




// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return the maximum element
static 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 code
public static void Main()
{
    int []arr = { 6,5, 1, 2, 3, 4 };
    int n = arr.Length;
     
    Console.WriteLine(findMax(arr, 0, n - 1));
}
}

PHP




<?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[$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);

Javascript




<script>
// Java script implementation of the approach
 
// Function to return the maximum element
function 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>
Output: 
6

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :