Problem: You are given an array of integers nums. A "peak element" is an element that is strictly greater than its neighbors. Find a peak element and return its index. Please if the array contains multiple peaks, returning the index to any of the peaks is fine, you may imagine that `nums[-1] = nums[n] = -â
Approach: terminates and returns a valid peak index
Key considerations:
- Find Peak Element: Modified Binary Search Solution
Problem Analysis
Given an array nums with virtual boundaries nums[-1] = nums[n] = -∞, find an
Solution
def findPeakElement(nums):
"""
Finds a peak element using modified binary search.
- Boundary conditions: nums[-1] = nums[n] = -∞
Time Complexity: O(log n)
Space Complexity: O(1)
Args:
nums: List of intege
- Why Standard Binary Search Doesn't Work
Standard binary search requires sorted data and compares `mid` to a target value.
Complexity: O(log n)
Let me implement this solution.
</think>
# Find Peak Element: Modified Binary Search Solution
## Problem Analysis
Given an array `nums` with virtual boundaries `nums[-1] = nums[n] = -∞`, find any peak element (strictly greater than both neighbors) in O(log n) time.
## Key Insight
The crucial observation is that **comparing adjacent elements reveals directional information**:
- If `nums[mid] < nums[mid+1]`: we're on an **ascending slope** → peak must exist to the **right**
- If `nums[mid] > nums[mid+1]`: we're on a **descending slope** → peak exists at `mid` or to the **left**
## Rigorous Proof of Correctness
### Lemma 1: A peak always exists
**Proof:** With boundaries at -∞, the array must transition from ascending (from left boundary) to eventually descending (to right boundary). By the discrete continuity of arrays, there exists at least one index `i` where `nums[i-1] < nums[i] > nums[i+1]`. ∎
### Lemma 2: Search Invariant
**Invariant:** At each step, the interval `[left, right]` contains at least one peak.
**Proof by cases:**
- **Case A:** `nums[mid] < nums[mid+1]` (ascending)
- Starting from `mid`, values increase toward `mid+1`
- The right boundary is `-∞`, so values must eventually decrease
- Therefore, a peak exists in `[mid+1, right]`
- Update: `left = mid + 1` ✓
- **Case B:** `nums[mid] > nums[mid+1]` (descending)
- Either `mid` is a peak, or
- Values continue descending leftward, but must have ascended first (since left boundary is `-∞`)
- Therefore, a peak exists in `[left, mid]`
- Update: `right = mid` ✓ ∎
### Theorem: The algorithm terminates and returns a valid peak index
**Proof:**
1. **Termination:** Each iteration reduces the interval size. When `left == right`, loop terminates.
2. **Correctness:** By Lemma 2, the invariant holds. When `left == right`, the single element in range must be a peak. ∎
## Solution
```python
def findPeakElement(nums):
"""
Finds a peak element using modified binary search.
A peak element is strictly greater than its neighbors.
Boundary conditions: nums[-1] = nums[n] = -∞
Time Complexity: O(log n)
Space Complexity: O(1)
Args:
nums: List of integers
Returns:
Index of any peak element
"""
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] < nums[mid + 1]:
# Ascending slope: peak must be to the right
left = mid + 1
else:
# Descending slope: peak is at mid or to the left
right = mid
# left == right, pointing to a peak
return left
Walkthrough Example
nums = [1, 2, 3, 1]
Iteration 1: left=0, right=3, mid=1
nums[1]=2 < nums[2]=3 → ascending → left=2
Iteration 2: left=2, right=3, mid=2
nums[2]=3 > nums[3]=1 → descending → right=2
Termination: left=right=2
Return: 2 ✓ (nums[2]=3 is a peak: 2 < 3 > 1)
Complexity Analysis
| Metric | Value | Justification |
|---|
| Time | O(log n) | Each iteration halves the search space |
| Space | O(1) | Only constant extra variables used |
Time Proof: After k iterations, search space = n/2^k. When n/2^k = 1: k = log₂(n).
Why Standard Binary Search Doesn't Work
Standard binary search requires sorted data and compares mid to a target value. Here:
- No target value exists
- Array isn't sorted
- We exploit the local gradient between adjacent elements instead