
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the maximum element in an array which is first increasing and then decreasing in C++
Suppose we have one array, which is initially increasing then decreasing. We have to find the max value in the array. So if the array elements are like A = [8, 10, 20, 80, 100, 250, 450, 100, 3, 2, 1], then output will be 500.
We can use the binary search to solve this. There are three conditions −
- When mid is greater than both of its adjacent elements, then mid is maximum
- If mid is greater than the next element, but smaller than previous element, then max lies on the left side of mid.
- If mid element is smaller than the next element, but greater than previous element, then max lies on the right side of mid.
Example
#include<iostream> using namespace std; int getMaxElement(int array[], int left, int right) { if (left == right) return array[left]; if ((right == left + 1) && array[left] >= array[right]) return array[left]; if ((right == left + 1) && array[left] < array[right]) return array[right]; int mid = (left + right)/2; if ( array[mid] > array[mid + 1] && array[mid] > array[mid - 1]) return array[mid]; if (array[mid] > array[mid + 1] && array[mid] < array[mid - 1]) return getMaxElement(array, left, mid-1); else return getMaxElement(array, mid + 1, right); } int main() { int array[] = {8, 10, 20, 80, 100, 250, 450, 100, 3, 2, 1}; int n = sizeof(array)/sizeof(array[0]); cout << "The maximum element is: " << getMaxElement(array, 0, n-1); }
Output
The maximum element is: 450
- Related Articles
- Count permutations that are first decreasing then increasing in C++
- Find an element in an array such that elements form a strictly decreasing and increasing sequence in Python
- C# program to find maximum and minimum element in an array\n
- Print array elements in alternatively increasing and decreasing order in C++
- Maximum difference between first and last indexes of an element in array in C
- Program to find maximum element after decreasing and rearranging in Python
- Find the first repeating element in an array of integers C++
- Program to check right rotation forms increasing or decreasing array with first n natural numbers or not in Python
- Program to find length of longest strictly increasing then decreasing sublist in Python
- How to find the minimum and maximum element of an Array using STL in C++?
- Strictly increasing or decreasing array - JavaScript
- How to find the maximum element of an Array using STL in C++?
- Program to find the minimum (or maximum) element of an array in C++
- PHP program to find the maximum element in an array
- C++ Program to Find Maximum Element in an Array using Binary Search

Advertisements