# Find the maximum element in an array which is first increasing and then decreasing

In this tutorial, we will learn how to search the maximum element of an array which is first increasing & then decreasing. This maximum element in such type of array is also known as peak element. By Radib Kar Last updated : August 10, 2023

## Problem statement

Say, the array is like below,

**[3, 10, 17, 15, 2, -3]**

Then the peak element or the maximum element would be **17**.

We can solve this both by linear search & binary search. The linear search algorithm will have the worst case time complexity of *O(n)* & the binary search algorithm will have worst-case time complexity of *O(logN)*.

## Using linear search

Since the array is increasing first & then decreasing so the maximum element would be the last one in the increasing series & the first one in the decreasing series. SO we can ignore the increasing part and check if the next element is decreasing or not. As soon as we find any element is greater than it's immediate next element on the right side, we can infer that as the peak element. The algorithm would ve like below,

for (int i = 1; i < arr.size(); i++) { if (arr[i] > arr[i + 1]) { peakNumber = arr[i]; break; } }

## Using binary search

We can also solve this using binary search. We need to check whether our pivot element is in the increasing part or the decreasing part. If the pivot element is in the increasing part then we need to search only in the right part & if it's in the decreasing part then we need to search in the left part. The implementation is like following,

int findThePeakEfficientRecur(vector& arr, int start, int end) { //base cases //only one element in array if (start == end) return arr[start]; //if two element if (start + 1 == end) { return max(arr[start], arr[end]); } int mid = start + (end - start) / 2; //we got our peak if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) return arr[mid]; //pivot element is still in the increasing part else if (arr[mid] > arr[mid - 1] && arr[mid] < arr[mid + 1]) { return findThePeakEfficientRecur(arr, mid + 1, end); } else return findThePeakEfficientRecur(arr, start, mid - 1); }

## C++ program to find the maximum element in an array which is first increasing and then decreasing

#include <bits/stdc++.h> using namespace std; //naive approach void findThePeakNaive(vector<int>& arr) { cout << "..............Using naive search......\n"; //O(n) time complexity int peakNumber = -1; //if only one element if (arr.size() == 1) peakNumber = arr[0]; //if two elements if (arr.size() == 2) peakNumber = max(arr[0], arr[1]); for (int i = 1; i < arr.size(); i++) { if (arr[i] > arr[i + 1]) { peakNumber = arr[i]; break; } } if (peakNumber == -1) cout << "There is no peak element in this array\n"; else cout << "The peak element(maximum number) is " << peakNumber << "\n"; return; } //recursive binary search int findThePeakEfficientRecur(vector<int>& arr, int start, int end) { //base cases //only one element in array if (start == end) return arr[start]; //if two element if (start + 1 == end) { return max(arr[start], arr[end]); } int mid = start + (end - start) / 2; //we got our peak if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) return arr[mid]; //pivot element is still in the increasing part else if (arr[mid] > arr[mid - 1] && arr[mid] < arr[mid + 1]) { return findThePeakEfficientRecur(arr, mid + 1, end); } else return findThePeakEfficientRecur(arr, start, mid - 1); } void findThePeakEfficient(vector<int>& arr) { cout << "..............Using efficient approach(binary search)......\n"; //O(logn) time complexity int peakNumber = findThePeakEfficientRecur(arr, 0, arr.size() - 1); if (peakNumber == -1) cout << "There is no peak element in this array\n"; else cout << "The peak element(maximum number) is " << peakNumber << "\n"; return; } int main() { cout << "Enter number of elements\n"; int n; cin >> n; vector<int> arr(n); cout << "Enter the elements(increasing first & then decreasing)\n"; for (int i = 0; i < n; i++) cin >> arr[i]; //using navive approach findThePeakNaive(arr); //using binary search findThePeakEfficient(arr); return 0; }

## Output

Enter number of elements 6 Enter the elements(increasing first & then decreasing) 3 10 17 15 2 -3 ..............Using naive search...... The peak element(maximum number) is 17 ..............Using efficient approach(binary search)...... The peak element(maximum number) is 17

Related Tutorials

- Introduction to Algorithms
- Introduction to Greedy Strategy in Algorithms
- Stability in sorting
- External Merge Sorting Algorithm
- Radix Sort and its Algorithm
- Bucket Sort Algorithm
- Bubble sort Algorithm, Flow Chart and C++ Code
- Insertion sort Algorithm, flowchart and C, C++ Code
- Merge Sort | One of the best sorting algorithms used for large inputs
- Binary Search in C, C++
- Randomized Binary Search
- Meta Binary Search | One-sided Binary Search
- Difference between Linear Search and Binary Search
- Binary Search in String
- Variants of Binary Search
- Sieve of Eratosthenes to find prime numbers
- Optimal Merge Pattern (Algorithm and Example)
- Given an array of n numbers, Check whether there is any duplicate or not
- Finding the missing number
- Find the number occurring an odd number of times
- Find the pair whose sum is closest to zero in minimum time complexity
- Find three elements in an array such that their sum is equal to given element K
- Bitonic Search Algorithm
- Check whether a number is Fibonacci or not
- Segregate even and odd numbers in minimum time complexity
- Find trailing zeros in factorial of a number
- Find Nearest Greatest Neighbours of each element in an array
- Interpolation search algorithm
- Floor and ceil of an element in an array using C++
- Two Elements whose sum is closest to zero
- Find a pair with a given difference
- Count number of occurrences (or frequency) in a sorted array
- Find a Fixed Point (Value equal to index) in a given array
- Dynamic Programming (Components, Applications and Elements)
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Dynamic Programming (Components, Applications and Elements)
- Find the Nth Fibonacci number | C++
- Longest Common Subsequence using Dynamic programming (DP)
- Longest Increasing Subsequence using Dynamic programming (DP)
- Find the maximum sub-array sum using KADANE'S ALGORITHM
- Non-intersecting chords using Dynamic Programming (DP)
- Edit Distance using Dynamic Programming (DP)
- Finding Ugly Number using Dynamic Programming (DP)
- Egg dropping problem using Dynamic Programming (DP)
- Wild card matching problem using Dynamic programming (DP)
- Compute sum of digits in all numbers from 1 to N for a given N
- Minimum jumps required using Dynamic programming (DP)
- Graph coloring problem's solution using backtracking algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- Travelling Salesman Problem
- Kruskal's (P) and Prim's (K) Algorithms
- Multistage graph problem with forward approach and backward approach algorithms
- Floyd Warshall algorithm with its Pseudo Code

- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Find the GCD (Greatest Common Divisor) of two numbers using EUCLID'S ALGORITHM
- Compute the value of A raise to the power B using Fast Exponentiation
- Implement First Come First Served (FCFS) CPU Scheduling Algorithm using C program
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Analysis of LRU page replacement algorithm and Belady's anomaly
- Branch and Bound
- Find the roots of a complex polynomial equation using Regula Falsi Method in C
- Sieve of Eratosthenes to find prime numbers
- Implementations of FCFS scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Non-Preemptive CPU scheduling algorithm using C++
- Implementation of Shortest Job First (SJF) Preemptive CPU scheduling algorithm using C++
- Implementation of Priority scheduling (Pre-emptive) algorithm using C++
- Divide and Conquer Paradigm (What it is, Its Applications, Pros and Cons)
- Implementation of Priority scheduling (Non Pre-emptive) algorithm using C++
- Implementation of Round Robin CPU Scheduling algorithm using C++
- Jump Search Implementation using C++
- Optimal Merge Pattern (Algorithm and Example)
- Introduction to Greedy Strategy in Algorithms
- Strassen's Matrix Multiplication in algorithms
- Huffman Coding (Algorithm, Example and Time complexity)
- Backtracking (Types and Algorithms)
- 4 Queen's problem and solution using backtracking algorithm
- N Queen's problem and solution using backtracking algorithm
- Graph coloring problem's solution using backtracking algorithm
- Tournament Tree and their properties
- Deterministic and Non Deterministic Algorithms
- Lower Bound Theory
- Non Recursive Tree Traversal Algorithm
- Line Drawing Algorithm
- Breadth First Search (BFS) and Depth First Search (DFS) Algorithms
- P and NP problems and solutions | Algorithms
- Travelling Salesman Problem
- 2 – 3 Trees Algorithm
- Kruskal's (P) and Prim's (K) Algorithms
- Algorithm for fractional knapsack problem
- Algorithm and procedure to solve a longest common subsequence problem
- Midpoint Circle Algorithm
- Multistage graph problem with forward approach and backward approach algorithms
- Floyd Warshall algorithm with its Pseudo Code
- Reliability design problem
- Removing consecutive duplicates from a string
- Fast Exponentiation using Bitmasking

Comments and Discussions!