Hello everyone, in this article, we will discuss the famous problem of binary search: Find peek element leetcode (https://leetcode.com/problems/find-peak-element/). We will see the problem explanation, solution approaches, and C++ and python code. This is a leetcode medium-level problem.

**Problem Explanation** to Find Peak Element leetcode

This problem is very straightforward, we have to find peak element in the given array, and if there is more than one peak element we can return any one.

Let’s first what a peak element is.

The peak element in an array is an element greater than its adjacent elements.

Here, 5 is the peak element because it is bigger than its adjacent elements. If the element is an endpoint element, we only have to compare it with only with one adjacent element.

In the above example, there is no peak element.

According to the problem statement, we will always have a solution.

**Solution Approaches** to leetcode 162

In this article, we will see two approaches to the question

- O(n) Approach
- O(logn) Approach

### 1. O(n) Approach to Find Peak Element in LeetCode problem:

In this approach, we compare each element with its adjacent elements and if it is a peak element we will return the element. In this approach, we also have to take care of endpoint elements separately.

We will have two corner cases:

- For 1st element
- For the last element

For these corner cases, we have to check separately.

**C++ Code:**

int findPeakElement(vector<int>& nums) { int n = nums.size(); if(n == 1) return 0; if(n == 2)return nums[0] > nums[1] ? 0 : 1; for(int i = 1; i < n-1; i++) if(nums[i] > nums[i-1] && nums[i] > nums[i+1]) return i; if(nums[0] > nums[1]) return 0; else if(nums[n-1] > nums[n-2]) return n-1; return -1; }

**Python Code:**

def findPeakElement(self, nums: List[int]) -> int: n = len(nums) for i in range(n): if (i == 0 or nums[i-1] < nums[i]) and (i == n-1 or nums[i] > nums[i+1]): # Found peak return i return -1

### 2. O(logn) Approach to Find Peak Element in LeetCode problem:

This approach simply uses binary search, there’s one small trick.

We will implement binary search as we use low and high variables to find the mid.

After finding the mid, we will compare the mid element with both its adjacent elements if the mid element is not the peak element,

**We shift towards the higher because there are more chances we will find our peak element towards the higher element.**

**C++ code:**

int findPeakElement(vector<int>& nums) { int n = nums.size(); int low = 0, high = n-1; if(n==1) return 0; while(low<=high) { int mid = low + (high-low)/2; int prev = (mid+n-1)%n; int next = (mid+1)%n; if(mid>0 && mid<n-1) { if(nums[mid] > nums[next] && nums[mid] > nums[prev]) { int res = nums[mid]; return mid; } else if(nums[next]>nums[mid]) { low = mid+1; } else { high = mid-1; } } else if(mid ==0) { if (nums[0]>nums[1]) return 0; else return 1; } else{ if (nums[n-1]>nums[n-2]) return n-1; else return n-2; } } return -1; }

**Python code:**

def findPeakElement(self, nums: List[int]) -> int: n = len(nums) left, right = 0, n - 1 while left <= right: mid = (left + right) // 2 if (mid == 0 or nums[mid-1] < nums[mid]) and (mid == n-1 or nums[mid] > nums[mid+1]): # Found peak return mid if mid == 0 or nums[mid-1] < nums[mid]: # Find peak on the right left = mid + 1 else: # Find peak on the left right = mid - 1 return -1

**Also Read:**

- HackerRank Day 8 Solution in Python: Dictionaries and Maps
- HackerRank Day 7 Solution in Python: Arrays
- HackerRank Day 6 Solution in Python: Let’s review
- HackerRank Day 5 Solution in Python: Loops
- HackerRank Day 4 Solution in Python: Class vs Instance
- HackerRank Day 3 Solution in Python: Intro to Conditional Statements
- HackerRank Day 2 Solution in Python: Operators
- HackerRank Day 1 Solution in Python: Data Types
- HackerRank Day 0 Solution in Python: Hello World
- HackerRank Day 29 Solution in Python: Bitwise AND
- HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases
- HackerRank Day 27 Solution in Python: Testing
- HackerRank Day 26 Solution in Python: Nested Logic
- HackerRank Day 25 Solution in Python: Running Time and Complexity
- HackerRank Day 24 Solution in Python: More Linked Lists
- HackerRank Day 23 Solution in Python: BST Level Order Traversal
- HackerRank Day 22 Solution in Python: Binary Search Trees
- Find Peak Element LeetCode 162
- HackerRank Day 20 Solution in Python: Sorting
- HackerRank Day 19 Solution in Python: Interfaces
- HackerRank Day 18 Solution in Python: Queues and Stacks
- HackerRank Day 17 Solution in Python: More Exceptions
- HackerRank Day 16 Solution: Exceptions – String to Integer
- Explained: Allocate minimum number of pages
- HackerRank Day 15 Solution in Python: Linked List
- Search a 2D matrix: leetcode 74
- Maximum Subarray Sum: Kadane’s Algorithm
- HackerRank Day 13 Solution in Python: Abstract Classes
- HackerRank Day 14 Solution in Python: Scope
- HackerRank Day 12 Solution in Python: Inheritance