162. Find Peak Element
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the index of any peak element in an integer array nums. A peak element is defined as an element that is strictly greater than its neighbors. The problem provides two crucial pieces of information:
Solution
Find Peak Element in Python
class Solution:
def findPeakElement(self, nums: List[int]) -> int:
left, right = 0, len(nums) - 1
# Binary search to find a peak element.
# The loop invariant is that a peak is guaranteed to be in the range [left, right].
while left < right:
mid = left + (right - left) // 2
# If the middle element is on an ascending slope,
# a peak must exist to its right.
if nums[mid] < nums[mid + 1]:
left = mid + 1
else:
# If the middle element is on a descending slope or is a peak,
# a peak exists at mid or to its left.
right = mid
# When the loop terminates, left == right, pointing to a peak element.
# This is because the search space [left, right] always contains a peak,
# and when it shrinks to a single element, that element must be a peak.
return left
Find Peak Element in Java
class Solution {
public int findPeakElement(int[] nums) {
int left = 0;
int right = nums.length - 1;
// Binary search to find a peak element.
// The loop invariant is that a peak is guaranteed to be in the range [left, right].
while (left < right) {
int mid = left + (right - left) / 2;
// If the middle element is on an ascending slope,
// a peak must exist to its right.
if (nums[mid] < nums[mid + 1]) {
left = mid + 1;
} else {
// If the middle element is on a descending slope or is a peak,
// a peak exists at mid or to its left.
right = mid;
}
}
// When the loop terminates, left == right, pointing to a peak element.
// This is because the search space [left, right] always contains a peak,
// and when it shrinks to a single element, that element must be a peak.
return left;
}
}
Find Peak Element in C++
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int left = 0;
int right = nums.size() - 1;
// Binary search to find a peak element.
// The loop invariant is that a peak is guaranteed to be in the range [left, right].
while (left < right) {
int mid = left + (right - left) / 2;
// If the middle element is on an ascending slope,
// a peak must exist to its right.
if (nums[mid] < nums[mid + 1]) {
left = mid + 1;
} else {
// If the middle element is on a descending slope or is a peak,
// a peak exists at mid or to its left.
right = mid;
}
}
// When the loop terminates, left == right, pointing to a peak element.
// This is because the search space [left, right] always contains a peak,
// and when it shrinks to a single element, that element must be a peak.
return left;
}
};
Find Peak Element in TypeScript
function findPeakElement(nums: number[]): number {
let left = 0;
let right = nums.length - 1;
// Binary search to find a peak element.
// The loop invariant is that a peak is guaranteed to be in the range [left, right].
while (left < right) {
// Use Math.floor for integer division.
const mid = left + Math.floor((right - left) / 2);
// If the middle element is on an ascending slope,
// a peak must exist to its right.
if (nums[mid] < nums[mid + 1]) {
left = mid + 1;
} else {
// If the middle element is on a descending slope or is a peak,
// a peak exists at mid or to its left.
right = mid;
}
}
// When the loop terminates, left == right, pointing to a peak element.
// This is because the search space [left, right] always contains a peak,
// and when it shrinks to a single element, that element must be a peak.
return left;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.