Maximum Element in a Sorted and Rotated Array in C++



A sorted and rotated array is an array that is sorted in ascending or descending order and then rotated either left or right by a specific number of elements. There should exist exactly one pivot point around which the array is rotated.

In this article, our task is to find the maximum element in the given sorted and rotated array. We will use the following two approaches mentioned below:

Example

Here is an example of searching the maximum element in the given sorted and rotated array:

Case 1
The given array is: 30 40 50 10 20
Output
Maximum element = 50

Case 2
The given array is: 10 20 30 40 50
Output
Maximum element = 50

The linear search is a sequential search algorithm where every element of the array is traversed and compared with the key element to be found. Below is an implementation of the linear search algorithm to search the maximum element in the given sorted and rotated array:

  • In linear search, we iterate over all the elements of the array using the for loop and search if the current element is greater than its next element.
  • If the maximum element is not found in the above step, then we return the last element of the array as it is the maximum element.

Example

Here is an example of finding the maximum element in the given sorted and rotated array using the linear search:

#include <iostream>
using namespace std;

int maxEle(int arr[], int n)
{
   for (int i = 0; i < n - 1; i++)
   {
      if (arr[i] > arr[i + 1])
      {
         return arr[i];
      }
   }

   return arr[n - 1];
}

int main()
{
   int arr[] = {30, 40, 50, 10, 20};
   int n = sizeof(arr) / sizeof(arr[0]);

   cout << "Array: ";
   for (int i = 0; i < n; i++)
      cout << arr[i] << " ";
   cout << endl;

   cout << "Maximum: " << maxEle(arr, n) << endl;

   return 0;
}

The output of the above code is as follows:

The given array is: 30 40 50 10 20 
Maximum element = 50

In this approach, we have used the binary search to find the maximum element. Using binary search, we divide the array and search the maximum element in the smaller sub-arrays.

  • First, find the middle element of the given array.
  • Then check the left and right neighboring element of the middle element.
  • If the maximum element is not found in the above step i.e. the maximum element is neither the middle element nor its left and right neighbor element, then we check in the left and right sub-arrays.
  • If the first element of the array is less than the mid element, then the maximum lies in the right sub-array, otherwise the maximum element lies in the left sub-array. Below is the explanation of this step:
  • For example: 
    arr 1 = {60, 70, 80, 90, 10, 20, 30, 35, 40, 45, 50}
    arr[mid] = 20, arr[mid-1] = 10, arr[mid+1] = 30
    Here, max element is neither arr[mid] nor its neighbors.
    Using step 4 we check,
    arr[0] <= arr[mid] i.e 60 <= 20 is false
    => maximum element lies in the left sub-array.
    
    arr 2 = {60, 65, 75, 80, 85, 90, 10}
    arr[mid] = 80, arr[mid-1] = 75, arr[mid+1] = 85
    Here, max element is neither arr[mid] nor its neighbors.
    Using step 4 we check,
    arr[0] <= arr[mid] i.e 60 <= 80 is True
    => maximum element lies in the right sub-array.
            

Example

Here is an example of searching the maximum element in the given sorted and rotated array using the binary search:

#include <bits/stdc++.h>
using namespace std;

int maxEle(int *arr, int low, int high)
{
   if (high < low)
   {
      return arr[0];
   }
   if (high == low)
   {
      return arr[high];
   }

   int mid = low + (high - low) / 2;

   // Checking if mid is the maximum element
   // Maximum element will be greater than both its neighbors
   if (mid < high && arr[mid] > arr[mid + 1])
   {
      return arr[mid];
   }
   if (mid > low && arr[mid] < arr[mid - 1])
   {
      return arr[mid - 1];
   }

   // If left half is sorted, maximum is either arr[low] or in right half
   if (arr[low] <= arr[mid])
   {
      return maxEle(arr, mid + 1, high);
   }
   // If right half is sorted, maximum is in left half
   else
   {
      return maxEle(arr, low, mid - 1);
   }
}

int main()
{
   int arr[] = {30, 40, 50, 10, 20};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << "The given array is: ";
   for (int i = 0; i < n; i++)
   {
      cout << arr[i] << " ";
   }
   cout << endl;
   cout << "Maximum element = " << maxEle(arr, 0, n - 1) << endl;

   return 0;
}

The output of the above code is as follows:

The given array is: 30 40 50 10 20 
Maximum element = 50
Updated on: 2025-06-09T19:15:30+05:30

509 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements