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.
- 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++
#include <bits/stdc++.h>
using namespace std;
int findMax( int arr[], int low, int high)
{
if (high == low)
return arr[low];
int mid = low + (high - low) / 2;
if (mid==0 && arr[mid]>arr[mid+1])
{
return arr[mid];
}
if (mid < high && arr[mid + 1] < arr[mid] && mid>0 && arr[mid]>arr[mid-1]) {
return arr[mid];
}
if (arr[low] > arr[mid]) {
return findMax(arr, low, mid - 1);
}
else {
return findMax(arr, mid + 1, high);
}
}
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
class GFG
{
static int findMax( int arr[], int low, int high)
{
if (high == low)
return arr[low];
int mid = low + (high - low) / 2 ;
if (mid== 0 && arr[mid]>arr[mid+ 1 ])
{
return arr[mid];
}
if (arr[low] > arr[mid])
{
return findMax(arr, low, mid - 1 );
}
else
{
return findMax(arr, mid + 1 , high);
}
}
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
def findMax(arr, low, high):
if (high = = low):
return arr[low]
mid = low + (high - low) / / 2
if (mid = = 0 and arr[mid]>arr[mid + 1 ]):
return arr[mid]
if (mid < high and arr[mid + 1 ] < arr[mid] and mid> 0 and arr[mid]>arr[mid - 1 ]):
return arr[mid]
if (arr[low] > arr[mid]):
return findMax(arr, low, mid - 1 )
else :
return findMax(arr, mid + 1 , high)
arr = [ 6 , 5 , 4 , 3 , 2 , 1 ]
n = len (arr)
print (findMax(arr, 0 , n - 1 ))
|
C#
using System;
class GFG
{
static int findMax( int []arr,
int low, int high)
{
if (high == low)
return arr[low];
int mid = low + (high - low) / 2;
if (mid==0 && arr[mid]>arr[mid+1])
return arr[mid];
if (mid < high && arr[mid + 1] < arr[mid] && mid>0 && arr[mid]>arr[mid-1])
{
return arr[mid];
}
if (arr[low] > arr[mid])
{
return findMax(arr, low, mid - 1);
}
else
{
return findMax(arr, mid + 1, high);
}
}
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
function findMax( $arr , $low , $high )
{
if ( $high <= $low )
return $arr [ $low ];
$mid = $low + ( $high - $low ) / 2;
if ( $mid ==0 && $arr [ $mid ]> $arr [ $mid -1])
return $arr [0];
if ( $mid < $high && $arr [ $mid + 1] < $arr [ $mid ] && $mid > 0 && $arr [ $mid ]> $arr [ $mid -1])
{
return $arr [ $mid ];
}
if ( $arr [ $low ] > $arr [ $mid ])
{
return findMax( $arr , $low , $mid - 1);
}
else
{
return findMax( $arr , $mid + 1, $high );
}
}
$arr = array (5,6,1,2,3,4);
$n = sizeof( $arr );
echo findMax( $arr , 0, $n - 1);
|
Javascript
<script>
function findMax(arr,low,high)
{
if (high == low)
return arr[low];
let mid = low + (high - low) / 2;
if (mid==0 && arr[mid]>arr[mid+1])
{
return arr[mid];
}
if (mid < high && arr[mid + 1] < arr[mid] && mid>0 && arr[mid]>arr[mid-1])
{
return arr[mid];
}
if (arr[low] > arr[mid])
{
return findMax(arr, low, mid - 1);
}
else
{
return findMax(arr, mid + 1, high);
}
}
let arr = [ 5, 6, 1, 2, 3, 4 ];
let n = arr.length;
document.write(findMax(arr, 0, n-1 ));
</script>
|
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
Share your thoughts in the comments
Please Login to comment...