
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Maximum Element in an Array Using Binary Search in C++
The binary search works on the divide and conquer principle as it keeps dividing the array into half before searching. For applying the binary search algorithm, the given array should be sorted. Since the array is sorted we do not need to search the maximum element in the array. Here, the given array is a Bitonic array.
A bitonic array is an array in which the left sub-array has strictly increasing elements till it reaches the peak element and the right sub-array has strictly decreasing elements. For example: {10, 20, 30, 40, 35, 25}. In this example, the array is increasing till it reaches the maximum element (40) and then starts decreasing.
Example
The following example finds the maximum element present in a bitonic array using binary search algorithm:
Input: array = {20, 25, 40, 24, 19, 10} Output: Maximum element is: 40
Steps to Find Maximum Element in Bitonic Array using Binary Search
The steps to find the maximum element in a bitonic array are as follows:
- First, we declare a bitonic array of 5 elements. Then we have two pointer variables low and high that point to the starting and the last element of the array.
- Then, we find the mid element and compare the mid element with its adjacent elements.
- If the mid element is greater than its adjacent elements (left and right), then it is the maximum element of the array.
- If the mid element is not the highest element in the previous step, then we check if the mid element is less than the (mid+1)th element. If this condition is satisfied, then we search in the right sub-array and set the low pointer to mid + 1.
- Otherwise, we search the max element in the left sub-array if the max element is not found in the above two steps.
- We repeat the above 4 steps until the maximum element is found or while low < high.
C++ Program to Find Maximum Element in Bitonic Array using Binary Search
Here is the Code implementation of the above steps where we have used the binary search to find the maximum element in a bitonic array:
#include <iostream> using namespace std; int maxEle(int arr[], int n) { int low = 0, high = n - 1; while (low <= high) { int mid = low + (high - low) / 2; // Checking the adjacent elements of mid element // to check if the mid element is the maximum if ((mid == 0 || arr[mid] > arr[mid - 1]) && (mid == n - 1 || arr[mid] > arr[mid + 1])) { return arr[mid]; } // To search in the right of mid element if (mid < n - 1 && arr[mid] < arr[mid + 1]) { low = mid + 1; } // For searching to the left of mid element else { high = mid - 1; } } return -1; } int main() { int arr[] = {20, 25, 40, 24, 19}; int n = sizeof(arr) / sizeof(arr[0]); cout << "The given bitonic array is: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } int maxElement = maxEle(arr, n); cout << "\nMaximum element is: " << maxElement << endl; return 0; }
The output of the above code is as follows:
The given bitonic array is: 20 25 40 24 19 Maximum element is: 40