GeeksforGeeks App
Open App
Browser
Continue

# Find a peak element which is not smaller than its neighbours

Given an array arr[] of integers. Find a peak element i.e. an element that is not smaller than its neighbors.

Note: For corner elements, we need to consider only one neighbor.

Example:

Input: array[]= {5, 10, 20, 15}
Output: 20
Explanation: The element 20 has neighbors 10 and 15, both of them are less than 20.

Input: array[] = {10, 20, 15, 2, 23, 90, 67}
Output: 20 or 90
Explanation: The element 20 has neighbors 10 and 15, both of them are less than 20, similarly 90 has neighbors 23 and 67.

The following corner cases give a better idea about the problem.

1. If the input array is sorted in a strictly increasing order, the last element is always a peak element. For example, 50 is peak element in {10, 20, 30, 40, 50}.
2. If the input array is sorted in a strictly decreasing order, the first element is always a peak element. 100 is the peak element in {100, 80, 60, 50, 20}.
3. If all elements of the input array are the same, every element is a peak element.

It is clear from the above examples that there is always a peak element in the input array.

Recommended Practice

Naive Approach: Below is the idea to solve the problem

The array can be traversed and the element whose neighbors are less than that element can be returned.

Follow the below steps to Implement the idea:

• If the first element is greater than the second or the last element is greater than the second last, print the respective element and terminate the program.
• Else traverse the array from the second index to the second last index i.e. 1 to N – 1
• If for an element array[i] is greater than both its neighbors, i.e.,  and, then print that element and terminate.

Below is the implementation of above idea.

## C++

 `// A C++ program to find a peak element``#include ``using` `namespace` `std;` `// Find the peak element in the array``int` `findPeak(``int` `arr[], ``int` `n)``{``    ``// first or last element is peak element``    ``if` `(n == 1)``        ``return` `0;``    ``if` `(arr[0] >= arr[1])``        ``return` `0;``    ``if` `(arr[n - 1] >= arr[n - 2])``        ``return` `n - 1;` `    ``// check for every other element``    ``for` `(``int` `i = 1; i < n - 1; i++) {` `        ``// check if the neighbors are smaller``        ``if` `(arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])``            ``return` `i;``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 3, 20, 4, 1, 0 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``cout << ``"Index of a peak point is "` `<< findPeak(arr, n);``    ``return` `0;``}` `// This code is contributed by Aditya Kumar (adityakumar129)`

## C

 `// A C program to find a peak element``#include ` `// Find the peak element in the array``int` `findPeak(``int` `arr[], ``int` `n)``{``    ``// first or last element is peak element``    ``if` `(n == 1)``        ``return` `0;``    ``if` `(arr[0] >= arr[1])``        ``return` `0;``    ``if` `(arr[n - 1] >= arr[n - 2])``        ``return` `n - 1;` `    ``// check for every other element``    ``for` `(``int` `i = 1; i < n - 1; i++) {` `        ``// check if the neighbors are smaller``        ``if` `(arr[i] >= arr[i - 1] && arr[i] >= arr[i + 1])``            ``return` `i;``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 3, 20, 4, 1, 0 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``printf``(``"Index of a peak point is %d"``,findPeak(arr, n));``    ``return` `0;``}` `// This code is contributed by Aditya Kumar (adityakumar129)`

## Java

 `// A Java program to find a peak element``import` `java.util.*;` `class` `GFG {` `    ``// Find the peak element in the array``    ``static` `int` `findPeak(``int` `arr[], ``int` `n)``    ``{``        ``// First or last element is peak element``        ``if` `(n == ``1``)``            ``return` `0``;``        ``if` `(arr[``0``] >= arr[``1``])``            ``return` `0``;``        ``if` `(arr[n - ``1``] >= arr[n - ``2``])``            ``return` `n - ``1``;``        ``// Check for every other element``        ``for` `(``int` `i = ``1``; i < n - ``1``; i++) {``            ``// Check if the neighbors are smaller``            ``if` `(arr[i] >= arr[i - ``1``] && arr[i] >= arr[i + ``1``])``                ``return` `i;``        ``}``        ``return` `0``;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = { ``1``, ``3``, ``20``, ``4``, ``1``, ``0` `};``        ``int` `n = arr.length;``        ``System.out.print(``"Index of a peak point is "` `+ findPeak(arr, n));``    ``}``}` `// This code is contributed by Aditya Kumar (adityakumar129)`

## Python3

 `# A Python3 program to find a peak element` `# Find the peak element in the array``def` `findPeak(arr, n) :` `    ``# first or last element is peak element``    ``if` `(n ``=``=` `1``) :``      ``return` `0``    ``if` `(arr[``0``] >``=` `arr[``1``]) :``        ``return` `0``    ``if` `(arr[n ``-` `1``] >``=` `arr[n ``-` `2``]) :``        ``return` `n ``-` `1`` ` `    ``# check for every other element``    ``for` `i ``in` `range``(``1``, n ``-` `1``) :`` ` `        ``# check if the neighbors are smaller``        ``if` `(arr[i] >``=` `arr[i ``-` `1``] ``and` `arr[i] >``=` `arr[i ``+` `1``]) :``            ``return` `i``            ` `# Driver code.``arr ``=` `[ ``1``, ``3``, ``20``, ``4``, ``1``, ``0` `]``n ``=` `len``(arr)``print``(``"Index of a peak point is"``, findPeak(arr, n))` `# This code is contributed by divyeshrabadiya07`

## C#

 `// A C# program to find a peak element``using` `System;` `public` `class` `GFG{` `// Find the peak element in the array``static` `int` `findPeak(``int` `[]arr, ``int` `n)``{``    ` `    ``// First or last element is peak element``    ``if` `(n == 1)``      ``return` `0;``    ``if` `(arr[0] >= arr[1])``        ``return` `0;``    ``if` `(arr[n - 1] >= arr[n - 2])``        ``return` `n - 1;` `    ``// Check for every other element``    ``for``(``int` `i = 1; i < n - 1; i++)``    ``{``        ` `        ``// Check if the neighbors are smaller``        ``if` `(arr[i] >= arr[i - 1] &&``            ``arr[i] >= arr[i + 1])``            ``return` `i;``    ``}``    ``return` `0;``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int` `[]arr = { 1, 3, 20, 4, 1, 0 };``    ``int` `n = arr.Length;``    ` `    ``Console.Write(``"Index of a peak point is "` `+``                     ``findPeak(arr, n));``}``}` ` `  `// This code is contributed by 29AjayKumar`

## Javascript

 ``

Output

`Index of a peak point is 2`

Time complexity: O(n), One traversal is needed so the time complexity is O(n)

Auxiliary Space: O(1), No extra space is needed, so space complexity is constant

## Find a peak element using recursive Binary Search

Below is the idea to solve the problem.

Using Binary Search, check if the middle element is the peak element or not. If the middle element is not the peak element, then check if the element on the right side is greater than the middle element then there is always a peak element on the right side. If the element on the left side is greater than the middle element then there is always a peak element on the left side

Follow the steps below to implement the idea:

• Create two variables, l and r, initialize l = 0 and r = n-1
• Recursively perform the below steps till l <= r, i.e. lowerbound is less than the upperbound
• Check if the mid value or index  is the peak element or not, if yes then print the element and terminate.
• Else if the element on the left side of the middle element is greater then check for peak element on the left side, i.e. update r = mid – 1
• Else if the element on the right side of the middle element is greater then check for peak element on the right side, i.e. update

Below is the implementation of the above approach

## C++

 `// A C++ program to find a peak element``// using divide and conquer``#include ``using` `namespace` `std;` `// A binary search based function``// that returns index of a peak element``int` `findPeakUtil(``int` `arr[], ``int` `low,``                 ``int` `high, ``int` `n)``{``    ``// Find index of middle element``    ``// low + (high - low) / 2``    ``int` `mid = low + (high - low) / 2;` `    ``// Compare middle element with its``    ``// neighbours (if neighbours exist)``    ``if` `((mid == 0 || arr[mid - 1] <= arr[mid]) &&``        ``(mid == n - 1 || arr[mid + 1] <= arr[mid]))``        ``return` `mid;` `    ``// If middle element is not peak and its``    ``// left neighbour is greater than it,``    ``// then left half must have a peak element``    ``else` `if` `(mid > 0 && arr[mid - 1] > arr[mid])``        ``return` `findPeakUtil(arr, low, (mid - 1), n);` `    ``// If middle element is not peak and its``    ``// right neighbour is greater than it,``    ``// then right half must have a peak element``    ``else``        ``return` `findPeakUtil(``            ``arr, (mid + 1), high, n);``}` `// A wrapper over recursive function findPeakUtil()``int` `findPeak(``int` `arr[], ``int` `n)``{``    ``return` `findPeakUtil(arr, 0, n - 1, n);``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 3, 20, 4, 1, 0 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``cout << ``"Index of a peak point is "``         ``<< findPeak(arr, n);``    ``return` `0;``}` `// This code is contributed by rajdeep999`

## C

 `// C program to find a peak``// element using divide and conquer``#include ` `// A binary search based function that``// returns index of a peak element``int` `findPeakUtil(``    ``int` `arr[], ``int` `low, ``int` `high, ``int` `n)``{``    ``// Find index of middle element``    ``// low + (high - low) / 2``    ``int` `mid = low + (high - low) / 2;` `    ``// Compare middle element with``    ``// its neighbours (if neighbours exist)``    ``if` `((mid == 0 || arr[mid - 1] <= arr[mid]) && (mid == n - 1 || arr[mid + 1] <= arr[mid]))``        ``return` `mid;` `    ``// If middle element is not peak and``    ``// its left neighbour is greater``    ``// than it, then left half must have a peak element``    ``else` `if` `(mid > 0 && arr[mid - 1] > arr[mid])``        ``return` `findPeakUtil(arr, low, (mid - 1), n);` `    ``// If middle element is not peak and``    ``// its right neighbour is greater``    ``// than it, then right half must have a peak element``    ``else``        ``return` `findPeakUtil(arr, (mid + 1), high, n);``}` `// A wrapper over recursive function findPeakUtil()``int` `findPeak(``int` `arr[], ``int` `n)``{``    ``return` `findPeakUtil(arr, 0, n - 1, n);``}` `/* Driver program to check above functions */``int` `main()``{``    ``int` `arr[] = { 1, 3, 20, 4, 1, 0 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``printf``(``        ``"Index of a peak point is %d"``, findPeak(arr, n));``    ``return` `0;``}`

## Java

 `// A Java program to find a peak``// element using divide and conquer``import` `java.util.*;``import` `java.lang.*;``import` `java.io.*;` `class` `PeakElement {``    ``// A binary search based function``    ``// that returns index of a peak element``    ``static` `int` `findPeakUtil(``        ``int` `arr[], ``int` `low, ``int` `high, ``int` `n)``    ``{``        ``// Find index of middle element``        ``// low + (high - low) / 2``        ``int` `mid = low + (high - low) / ``2``;` `        ``// Compare middle element with its``        ``// neighbours (if neighbours exist)``        ``if` `((mid == ``0` `|| arr[mid - ``1``] <= arr[mid])``            ``&& (mid == n - ``1` `|| arr[mid + ``1``] <= arr[mid]))``            ``return` `mid;` `        ``// If middle element is not peak``        ``// and its left neighbor is``        ``// greater than it, then left half``        ``// must have a peak element``        ``else` `if` `(mid > ``0` `&& arr[mid - ``1``] > arr[mid])``            ``return` `findPeakUtil(arr, low, (mid - ``1``), n);` `        ``// If middle element is not peak``        ``// and its right neighbor``        ``// is greater than it, then right``        ``// half must have a peak``        ``// element``        ``else``            ``return` `findPeakUtil(``                ``arr, (mid + ``1``), high, n);``    ``}` `    ``// A wrapper over recursive function``    ``// findPeakUtil()``    ``static` `int` `findPeak(``int` `arr[], ``int` `n)``    ``{``        ``return` `findPeakUtil(arr, ``0``, n - ``1``, n);``    ``}` `    ``// Driver method``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `arr[] = { ``1``, ``3``, ``20``, ``4``, ``1``, ``0` `};``        ``int` `n = arr.length;``        ``System.out.println(``            ``"Index of a peak point is "` `+ findPeak(arr, n));``    ``}``}`

## Python3

 `# A python3 program to find a peak``#  element using divide and conquer` `# A binary search based function``# that returns index of a peak element`  `def` `findPeakUtil(arr, low, high, n):` `    ``# Find index of middle element``    ``# low + (high - low) / 2``    ``mid ``=` `low ``+` `(high ``-` `low)``/``2``    ``mid ``=` `int``(mid)` `    ``# Compare middle element with its``    ``# neighbours (if neighbours exist)``    ``if` `((mid ``=``=` `0` `or` `arr[mid ``-` `1``] <``=` `arr[mid]) ``and``        ``(mid ``=``=` `n ``-` `1` `or` `arr[mid ``+` `1``] <``=` `arr[mid])):``        ``return` `mid`  `    ``# If middle element is not peak and``    ``# its left neighbour is greater``    ``# than it, then left half must``    ``# have a peak element``    ``elif` `(mid > ``0` `and` `arr[mid ``-` `1``] > arr[mid]):``        ``return` `findPeakUtil(arr, low, (mid ``-` `1``), n)` `    ``# If middle element is not peak and``    ``# its right neighbour is greater``    ``# than it, then right half must``    ``# have a peak element``    ``else``:``        ``return` `findPeakUtil(arr, (mid ``+` `1``), high, n)`  `# A wrapper over recursive``# function findPeakUtil()``def` `findPeak(arr, n):` `    ``return` `findPeakUtil(arr, ``0``, n ``-` `1``, n)`  `# Driver code``arr ``=` `[``1``, ``3``, ``20``, ``4``, ``1``, ``0``]``n ``=` `len``(arr)``print``(``"Index of a peak point is"``, findPeak(arr, n))``    ` `# This code is contributed by``# Smitha Dinesh Semwal`

## C#

 `// A C# program to find``// a peak element``// using divide and conquer``using` `System;` `class` `GFG {` `    ``// A binary search based``    ``// function that returns``    ``// index of a peak element``    ``static` `int` `findPeakUtil(``int``[] arr, ``int` `low,``                            ``int` `high, ``int` `n)``    ``{``        ``// Find index of``        ``// middle element mid = low + (high - low) / 2``        ``int` `mid = low + (high - low) / 2;` `        ``// Compare middle element with``        ``// its neighbours (if neighbours``        ``// exist)``        ``if` `((mid == 0 || arr[mid - 1] <= arr[mid]) && (mid == n - 1 || arr[mid + 1] <= arr[mid]))``            ``return` `mid;` `        ``// If middle element is not``        ``// peak and its left neighbor``        ``// is greater than it, then``        ``// left half must have a``        ``// peak element``        ``else` `if` `(mid > 0 && arr[mid - 1] > arr[mid])``            ``return` `findPeakUtil(arr, low,``                                ``(mid - 1), n);` `        ``// If middle element is not``        ``// peak and its right neighbor``        ``// is greater than it, then``        ``// right half must have a peak``        ``// element``        ``else``            ``return` `findPeakUtil(arr, (mid + 1),``                                ``high, n);``    ``}` `    ``// A wrapper over recursive``    ``// function findPeakUtil()``    ``static` `int` `findPeak(``int``[] arr,``                        ``int` `n)``    ``{``        ``return` `findPeakUtil(arr, 0,``                            ``n - 1, n);``    ``}` `    ``// Driver Code``    ``static` `public` `void` `Main()``    ``{``        ``int``[] arr = { 1, 3, 20,``                      ``4, 1, 0 };``        ``int` `n = arr.Length;``        ``Console.WriteLine(``"Index of a peak "``                          ``+ ``"point is "` `+ findPeak(arr, n));``    ``}``}` `// This code is contributed by ajit`

## PHP

 ` 0 &&``             ``\$arr``[``\$mid` `- 1] > ``\$arr``[``\$mid``])``        ``return` `findPeakUtil(``\$arr``, ``\$low``,``                           ``(``\$mid` `- 1), ``\$n``);` `    ``// If middle element is not peak``    ``// and its right neighbour is``    ``// greater than it, then right``    ``// half must have a peak element``    ``else` `return``(findPeakUtil(``\$arr``, (``\$mid` `+ 1),``                             ``\$high``, ``\$n``));``}` `// A wrapper over recursive``// function findPeakUtil()``function` `findPeak(``\$arr``, ``\$n``)``{``    ``return` `floor``(findPeakUtil(``\$arr``, 0,``                              ``\$n` `- 1, ``\$n``));``}` `// Driver Code``\$arr` `= ``array``(1, 3, 20, 4, 1, 0);``\$n` `= sizeof(``\$arr``);``echo` `"Index of a peak point is "``,``              ``findPeak(``\$arr``, ``\$n``);` `// This code is contributed by ajit``?>`

## Javascript

 `// A Javascript program to find a peak element``// using divide and conquer` `// A binary search based function``// that returns index of a peak element``function` `findPeakUtil(arr, low, high, n)``{``    ``// Find index of middle element``    ``// low + (high - low) / 2``    ``var` `mid = low + parseInt((high - low) / 2);` `    ``// Compare middle element with its``    ``// neighbours (if neighbours exist)``    ``if` `((mid == 0 || arr[mid - 1] <= arr[mid]) &&``        ``(mid == n - 1 || arr[mid + 1] <= arr[mid]))``        ``return` `mid;` `    ``// If middle element is not peak and its``    ``// left neighbour is greater than it,``    ``// then left half must have a peak element``    ``else` `if` `(mid > 0 && arr[mid - 1] > arr[mid])``        ``return` `findPeakUtil(arr, low, (mid - 1), n);` `    ``// If middle element is not peak and its``    ``// right neighbour is greater than it,``    ``// then right half must have a peak element``    ``else``        ``return` `findPeakUtil(``            ``arr, (mid + 1), high, n);``}` `// A wrapper over recursive function findPeakUtil()` `function` `findPeak(arr, n)``{``    ``return` `findPeakUtil(arr, 0, n - 1, n);``}` `// Driver Code``var` `arr = [ 1, 3, 20, 4, 1, 0 ];``var` `n = arr.length;``console.log(``"Index of a peak point is "``    ``+ findPeak(arr, n));`

Output

`Index of a peak point is 2`

Time Complexity: O(log N), Where N is the number of elements in the input array.
Auxiliary Space: O(log N), As recursive call is there, hence implicit stack is used.

## Find a peak element using iterative Binary search

Below is the idea to solve the problem.

Using Binary Search, check if the middle element is the peak element or not. If the middle element the peak element terminate the while loop and print middle element, then check if the element on the right side is greater than the middle element then there is always a peak element on the right side. If the element on the left side is greater than the middle element then there is always a peak element on the left side

Follow the steps below to implement the idea:

• Create two variables, l and r, initialize l = 0 and r = n-1
• Run a while loop till l <= r, lowerbound is less than the upperbound
• Check if the mid value or index  is the peak element or not, if yes then print the element and terminate.
• Else if the element on the left side of the middle element is greater then check for peak element on the left side, i.e. update r = mid – 1
• Else if the element on the right side of the middle element is greater then check for peak element on the right side, i.e. update

The below-given code is the iterative version of the above explained and demonstrated recursive based divide and conquer technique.

## C++

 `// A C++ program to find a peak element``// using divide and conquer``#include ``using` `namespace` `std;` `// A binary search based function``// that returns index of a peak element``int` `findPeak(``int` `arr[], ``int` `n)``{``    ``int` `l = 0;``    ``int` `r = n-1;``    ``int` `mid;``  ` `    ``while` `(l <= r) {``      ` `        ``// finding mid by binary right shifting.``        ``mid = (l + r) >> 1;``      ` `        ``// first case if mid is the answer``        ``if` `((mid == 0 || arr[mid - 1] <= arr[mid])``            ``and (mid == n - 1 || arr[mid + 1] <= arr[mid]))``            ``break``;``      ` `        ``// move the right pointer``        ``if` `(mid > 0 and arr[mid - 1] > arr[mid])``            ``r = mid - 1;``      ` `        ``// move the left pointer``        ``else``            ``l = mid + 1;``    ``}``  ` `    ``return` `mid;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 3, 20, 4, 1, 0 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``cout << ``"Index of a peak point is "` `<< findPeak(arr, N);``    ``return` `0;``}` `// This code is contributed by Rajdeep Mallick (rajdeep999)`

## C

 `// A C program to find a peak element using divide and``// conquer``#include ` `// A binary search based function``// that returns index of a peak element``int` `findPeak(``int` `arr[], ``int` `n)``{``    ``int` `l = 0;``    ``int` `r = n-1;``    ``int` `mid;``  ` `    ``while` `(l <= r) {``        ``// finding mid by binary right shifting.``        ``mid = (l + r) >> 1;``      ` `        ``// first case if mid is the answer``        ``if` `((mid == 0 || arr[mid - 1] <= arr[mid])``            ``&& (mid == n - 1 || arr[mid + 1] <= arr[mid]))``            ``break``;``      ` `        ``// move the right pointer``        ``if` `(mid > 0 && arr[mid - 1] > arr[mid])``            ``r = mid - 1;``      ` `        ``// move the left pointer``        ``else``            ``l = mid + 1;``    ``}``  ` `    ``return` `mid;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 1, 3, 20, 4, 1, 0 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``printf``(``"Index of a peak point is %d"``, findPeak(arr, n));``    ``return` `0;``}` `// This code is contributed by Rajdeep Mallick (rajdeep999)`

## Java

 `// A Java program to find a peak element using divide and``// conquer``import` `java.io.*;` `class` `GFG {` `    ``// A binary search based function that returns index of``    ``// a peak element``    ``static` `int` `findPeak(``int` `arr[], ``int` `n)``    ``{``        ``int` `l = ``0``;``        ``int` `r = n-``1``;``        ``int` `mid = ``0``;``      ` `        ``while` `(l <= r) {` `            ``// finding mid by binary right shifting.``            ``mid = (l + r) >> ``1``;` `            ``// first case if mid is the answer``            ``if` `((mid == ``0``                 ``|| arr[mid - ``1``] <= arr[mid])``                        ``&& (mid == n - ``1``                            ``|| arr[mid + ``1``] <= arr[mid]))``                ``break``;` `            ``// move the right pointer``            ``if` `(mid > ``0` `&& arr[mid - ``1``] > arr[mid])``                ``r = mid - ``1``;` `            ``// move the left pointer``            ``else``                ``l = mid + ``1``;``        ``}``      ` `        ``return` `mid;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `arr[] = { ``1``, ``3``, ``20``, ``4``, ``1``, ``0` `};``        ``int` `n = arr.length;``        ``System.out.println(``"Index of a peak point is "``                           ``+ findPeak(arr, n));``    ``}``}` `// This code is contributed by Rajdeep Mallick (rajdeep999)`

## Python3

 `# A Python program to find a peak element``# using divide and conquer` `# A binary search based function``# that returns index of a peak element``def` `findPeak(arr, n):``  ` `    ``l ``=` `0``    ``r ``=` `n``-``1``    ` `    ``while``(l <``=` `r):` `        ``# finding mid by binary right shifting.``        ``mid ``=` `(l ``+` `r) >> ``1` `        ``# first case if mid is the answer``        ``if``((mid ``=``=` `0` `or` `arr[mid ``-` `1``] <``=` `arr[mid]) ``and` `(mid ``=``=` `n ``-` `1` `or` `arr[mid ``+` `1``] <``=` `arr[mid])):``            ``break` `        ``# move the right pointer``        ``if``(mid > ``0` `and` `arr[mid ``-` `1``] > arr[mid]):``            ``r ``=` `mid ``-` `1` `        ``# move the left pointer``        ``else``:``            ``l ``=` `mid ``+` `1` `    ``return` `mid`  `# Driver Code``arr ``=` `[``1``, ``3``, ``20``, ``4``, ``1``, ``0``]``n ``=` `len``(arr)``print``(f``"Index of a peak point is {findPeak(arr, n)}"``)` `# This code is contributed by Rajdeep Mallick (rajdeep999)`

## C#

 `// A C# program to find a peak element``// using divide and conquer``using` `System;` `public` `class` `GFG {` `    ``// A binary search based function``    ``// that returns index of a peak element``    ``static` `int` `findPeak(``int``[] arr, ``int` `n)``    ``{``        ``int` `l = 0;``        ``int` `r = n-1;``        ``int` `mid = 0;``      ` `        ``while` `(l <= r) {` `            ``// finding mid by binary right shifting.``            ``mid = (l + r) >> 1;` `            ``// first case if mid is the answer``            ``if` `((mid == 0``                 ``|| arr[mid - 1] <= arr[mid]``                        ``&& (mid == n - 1``                            ``|| arr[mid + 1] <= arr[mid])))``                ``break``;` `            ``// move the right pointer``            ``if` `(mid > 0 && arr[mid - 1] > arr[mid])``                ``r = mid - 1;` `            ``// move the left pointer``            ``else``                ``l = mid + 1;``        ``}``      ` `        ``return` `mid;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int``[] arr = { 1, 3, 20, 4, 1, 0 };``        ``int` `n = arr.Length;``        ``Console.WriteLine(``"Index of a peak point is "``                          ``+ findPeak(arr, n));``    ``}``}` `// This code is contributed by Rajdeep Mallick (rajdeep999)`

## Javascript

 ``

Output

`Index of a peak point is 2`

Time Complexity: O(log N), Where n is the number of elements in the input array. In each step our search becomes half. So it can be compared to Binary search, So the time complexity is O(log N)
Auxiliary Space: O(1), No extra space is required, so the space complexity is constant.

Find a peak element using Binary search

Below is the idea to solve the problem.

Iterate a loop until start is less than end and find mid element. Check if the middle element is greater than middle +1 element then we are in decreasing part of array so we look in left to find peak index and set end = mid.Then, Check if the middle element is less than middle +1 element then we are in increasing part of array and to find peak index set start = mid + 1 . In the end ,start == end and pointing to the largest number because of the 2 checks above and return start or end to get the peak element.

Follow the steps below to implement the idea:

• Create two variables, start and end , initialize start = 0 and end = n-1
• Run a while loop till start < end such as:

Set mid = start + (end – start) / 2

Check if the arr[mid] > arr[mid+1] ,if yes then check for peak element on the left side and set end = mid.

Check if the arr[mid] < arr[mid+1] ,if yes then check for peak element on the right side and set start = mid+1.

• In the end, start == end and pointing to the largest number and return start or end to get the peak element.

## C++

 `#include ``using` `namespace` `std;` `int` `findPeak(``int` `arr[], ``int` `n) {``    ``int` `start = 0;``    ``int` `end = n - 1;` `    ``while` `(start < end) {``        ``int` `mid = start + (end - start) / 2;``        ``if` `(arr[mid] > arr[mid + 1]) {``            ``// You are in the decreasing part of the array.``            ``// This may be the answer, but look at the left side too.``            ``// This is why end != mid - 1.``            ``end = mid;``        ``}``        ``else` `{``            ``// You are in the increasing part of the array.``            ``// Because we know that mid+1 element > mid element.``            ``start = mid + 1;``        ``}``    ``}` `    ``// In the end, start == end and pointing to the largest number``    ``// because of the two checks above.``    ``// start and end are always trying to find the max element in``    ``// the above two checks, hence when they are pointing to just one``    ``// element, that is the max one because that is what the checks say.``    ``// More elaboration: at every point of time for start and end,``    ``// they have the best possible answer till that time and if we``    ``// are saying that only one item is remaining, hence because of``    ``// the above line that is the best possible answer.``    ``return` `start; ``// Or return end``}` `int` `main() {``    ``int` `arr[] = {1, 3, 20, 4, 1, 0};``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``cout << ``"Index of a peak point is "` `<< findPeak(arr, n) << endl;``    ``return` `0;``}`

## Java

 `// A Java program to find a peak element using divide and``// conquer``import` `java.util.*;` `class` `GFG {` `    ``// A binary search based function that returns index of``    ``// a peak element``    ``static` `int` `findPeak(``int` `arr[], ``int` `n)``    ``{``        ``int` `start = ``0``;``        ``int` `end = n - ``1``;` `        ``while` `(start < end) {``            ``int` `mid = start + (end - start) / ``2``;``            ``if` `(arr[mid] > arr[mid + ``1``]) {``                ``// you are in dec part of array``                ``// this may be the ans, but look at left``                ``// this is why end != mid - 1``                ``end = mid;``            ``}``            ``else` `{``                ``// you are in asc part of array``                ``start``                    ``= mid + ``1``; ``// because we know that mid+1``                               ``// element > mid element``            ``}``        ``}``        ``// in the end, start == end and pointing to the``        ``// largest number because of the 2 checks above``        ``// start and end are always trying to find max``        ``// element in the above 2 checks hence, when they``        ``// are pointing to just one element, that is the max``        ``// one because that is what the checks say more``        ``// elaboration: at every point of time for start and``        ``// end, they have the best possible answer till that``        ``// time and if we are saying that only one item is``        ``// remaining, hence cuz of above line that is the``        ``// best possible ans``        ``return` `start; ``// or return end``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String args[])``    ``{``        ``int` `arr[] = { ``1``, ``3``, ``20``, ``4``, ``1``, ``0` `};``        ``int` `n = arr.length;``        ``System.out.println(``"Index of a peak point is "``                           ``+ findPeak(arr, n));``    ``}``}`

## Python3

 `def` `findPeak(arr, n):``    ``start ``=` `0``    ``end ``=` `n ``-` `1` `    ``while` `start < end:``        ``mid ``=` `start ``+` `(end ``-` `start) ``/``/` `2``        ``if` `arr[mid] > arr[mid ``+` `1``]:``            ``# You are in the decreasing part of the array.``            ``# This may be the answer, but look at the left side too.``            ``# This is why end != mid - 1.``            ``end ``=` `mid``        ``else``:``            ``# You are in the increasing part of the array.``            ``# Because we know that mid+1 element > mid element.``            ``start ``=` `mid ``+` `1` `    ``# In the end, start == end and pointing to the largest number``    ``# because of the two checks above.``    ``# start and end are always trying to find the max element in``    ``# the above two checks, hence when they are pointing to just one``    ``# element, that is the max one because that is what the checks say.``    ``# More elaboration: at every point of time for start and end,``    ``# they have the best possible answer till that time and if we``    ``# are saying that only one item is remaining, hence because of``    ``# the above line that is the best possible answer.``    ``return` `start ``# Or return end` `# Driver Code``arr ``=` `[``1``, ``3``, ``20``, ``4``, ``1``, ``0``]``n ``=` `len``(arr)``print``(f``"Index of a peak point is {findPeak(arr, n)}"``)`

## Javascript

 `function` `findPeak(arr, n) {``  ``let start = 0;``  ``let end = n - 1;` `  ``while` `(start < end) {``    ``let mid = Math.floor(start + (end - start) / 2);``    ``if` `(arr[mid] > arr[mid + 1]) {``    ` `      ``// You are in the decreasing part of the array.``      ``// This may be the answer, but look at the left side too.``      ``// This is why end !== mid - 1.``      ``end = mid;``    ``} ``else` `{``      ``// You are in the increasing part of the array.``      ``// Because we know that mid+1 element > mid element.``      ``start = mid + 1;``    ``}``  ``}` `  ``// In the end, start === end and pointing to the largest number``  ``// because of the two checks above.``  ``// start and end are always trying to find the max element in``  ``// the above two checks, hence when they are pointing to just one``  ``// element, that is the max one because that is what the checks say.``  ``// More elaboration: at every point of time for start and end,``  ``// they have the best possible answer till that time and if we``  ``// are saying that only one item is remaining, hence because of``  ``// the above line that is the best possible answer.``  ``return` `start; ``// Or return end;``}` `// Driver Code``const arr = [1, 3, 20, 4, 1, 0];``const n = arr.length;``console.log(`Index of a peak point is \${findPeak(arr, n)}`);`

## C#

 `using` `System;` `class` `MainClass {``    ``public` `static` `int` `FindPeak(``int``[] arr, ``int` `n)``    ``{``        ``int` `start = 0;``        ``int` `end = n - 1;` `        ``while` `(start < end) {``            ``int` `mid = start + (end - start) / 2;``            ``if` `(arr[mid] > arr[mid + 1]) {``                ``// You are in the decreasing part of the``                ``// array. This may be the answer, but look``                ``// at the left side too. This is why end !=``                ``// mid - 1.``                ``end = mid;``            ``}``            ``else` `{``                ``// You are in the increasing part of the``                ``// array. Because we know that mid+1 element``                ``// > mid element.``                ``start = mid + 1;``            ``}``        ``}` `        ``// In the end, start == end and pointing to the``        ``// largest number because of the two checks above.``        ``// start and end are always trying to find the max``        ``// element in the above two checks, hence when they``        ``// are pointing to just one element, that is the max``        ``// one because that is what the checks say. More``        ``// elaboration: at every point of time for start and``        ``// end, they have the best possible answer till that``        ``// time and if we are saying that only one item is``        ``// remaining, hence because of the above line that``        ``// is the best possible answer.``        ``return` `start; ``// Or return end``    ``}` `    ``public` `static` `void` `Main()``    ``{``        ``int``[] arr = { 1, 3, 20, 4, 1, 0 };``        ``int` `n = arr.Length;``        ``Console.WriteLine(``"Index of a peak point is "``                          ``+ FindPeak(arr, n));``    ``}``}`

Output

`Index of a peak point is 2`

Time Complexity: O(log N), Where n is the number of elements in the input array. In each step our search becomes half. So it can be compared to Binary search, So the time complexity is O(log N)
Auxiliary Space: O(1), No extra space is required, so the space complexity is constant.

Exercise:
Consider the following modified definition of peak element. An array element is a peak if it is greater than its neighbors. Note that an array may not contain a peak element with this modified definition.

Related Problem:
Find local minima in an array

My Personal Notes arrow_drop_up