Find Peak Element - Complete Solution Guide
Find Peak Element is LeetCode problem 162, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
A peak element is an element that is strictly greater than its neighbors. Given a 0-indexed integer array nums , find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks . You may imagine that nums[-1] = nums[n] = -∞ . In other words, an element is always considered to be strictly greater than a neighbor that is outside the array. You must write an algorithm that runs in O(log n) time. Example 1: Input: nums = [1,2,3,1] Output: 2
Detailed Explanation
The problem asks us to find a 'peak element' within a given integer array. A peak element is defined as an element that is strictly greater than its adjacent neighbors. The array is 0-indexed. The crucial constraint is to find *any* peak element's index in O(log n) time. We can imagine that the array extends to negative infinity on both ends, meaning nums[-1] = nums[n] = -infinity.
Solution Approach
The solution utilizes a modified binary search algorithm to efficiently find a peak element. The core idea is that if `nums[mid] < nums[mid+1]`, then there must be a peak element in the right half of the array (including `mid+1`). Conversely, if `nums[mid] > nums[mid+1]`, then there must be a peak element in the left half of the array (including `mid`). By repeatedly halving the search space based on this comparison, we can quickly converge to a peak element.
Step-by-Step Algorithm
- Step 1: Initialize `low` to 0 and `high` to `len(nums) - 1`. These represent the bounds of our search space.
- Step 2: Enter a `while` loop that continues as long as `low < high`. This ensures that we have a search space of at least two elements.
- Step 3: Calculate the middle index `mid = low + (high - low) // 2`. This prevents potential integer overflow issues.
- Step 4: Compare `nums[mid]` with `nums[mid + 1]`. If `nums[mid] < nums[mid + 1]`, it implies that the slope is increasing, meaning a peak exists to the right of `mid`. Update `low = mid + 1`.
- Step 5: Otherwise, if `nums[mid] >= nums[mid + 1]`, it implies that the slope is decreasing or flat, meaning a peak exists at or to the left of `mid`. Update `high = mid`.
- Step 6: Repeat steps 3-5 until `low` and `high` converge. At this point, `low` (or `high`) will be the index of a peak element.
- Step 7: Return `low` (or `high`).
Key Insights
- Insight 1: The constraint of O(log n) strongly suggests using Binary Search.
- Insight 2: Even though the problem isn't explicitly sorted, the property that elements are not equal to their neighbors allows us to apply a modified binary search. The comparison between nums[mid] and nums[mid+1] is crucial for deciding which half of the array contains a peak.
- Insight 3: The condition nums[i] != nums[i+1] guarantees that there is always at least one peak element. This ensures that the binary search will always converge on a valid peak.
Complexity Analysis
Time Complexity: O(log n)
Space Complexity: O(1)
Topics
This problem involves: Array, Binary Search.
Companies
Asked at: Accenture, Commvault, Databricks, Flipkart, Goldman Sachs, IXL, Nvidia, Oracle, PayPal, ServiceNow, Snap, Tekion, TikTok, Visa, Waymo, Wix, Zepto, eBay.