Open In App

Maximum element in a sorted and rotated array

Improve
Improve
Like Article
Like
Save
Share
Report

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

 

Time Complexity: O(logn), where n represents the size of the given array.
Auxiliary Space: O(logn) due to recursive stack space.



Last Updated : 27 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads