Try the Question
Before reading the explanation, try solving the problem yourself:
👉 https://leetcode.com/problems/peak-index-in-a-mountain-array/
Solving it first helps strengthen your problem-solving intuition, especially for binary search problems.
Problem Statement
You are given an integer array arr that is guaranteed to be a mountain array.
A mountain array is defined as an array where:
- Elements strictly increase until a peak element.
- After the peak, elements strictly decrease.
Example:
Your task is to return the index of the peak element.
Constraints
Important implications:
- The peak always exists.
- There will be exactly one peak.
- The array strictly increases before the peak and strictly decreases after it.
Example Walkthrough
Example 1
Input
Visualization
Output
Example 2
Input
Visualization
Output
Example 3
Input
Visualization
Output
Understanding the Mountain Array Structure
A mountain array always looks like this:
Graphically:
Because of this structure:
- Before the peak → numbers increase
- After the peak → numbers decrease
This property makes the problem perfect for binary search.
Approach 1 — Brute Force (Check Every Element)
The simplest way is to check every element and determine if it is greater than its neighbors.
Algorithm
- Traverse the array from index
1ton-2. - For each element check:
- If true, return
i.
Implementation
Time Complexity
We may need to check every element.
Space Complexity
Approach 2 — Linear Scan Using Increasing Trend
Since the array first increases then decreases, we can detect where the increase stops.
Key Idea
Traverse the array and check when:
This means we reached the peak.
Implementation
Example
At index 3:
So index 3 is the peak.
Time Complexity
Space Complexity
Approach 3 — Modified Binary Search (Optimal Solution)
Because the array has a mountain structure, binary search can be used.
Core Intuition
Compare:
Two situations are possible.
Case 1 — Increasing Slope
Example:
This means the peak is on the right side.
Move the search space to the right.
Case 2 — Decreasing Slope
Example:
This means the peak lies on the left side including mid.
Optimal Java Implementation
Step-by-Step Example
Array
Iteration 1
Decreasing slope → move left.
Iteration 2
Search space narrows until:
Time Complexity
Binary search halves the search space each iteration.
Space Complexity
No extra memory is required.
Why Binary Search Works Here
Because the array behaves like a mountain curve.
If you are standing at index mid:
- If the right side is higher, go right.
- If the left side is higher, go left.
Eventually you will reach the top of the mountain (the peak).
Key Takeaways
✔ A mountain array increases then decreases
✔ There is exactly one peak
✔ Brute force and linear scan work in O(n) time
✔ Binary search exploits the mountain structure
✔ Optimal solution runs in O(log n) time
Final Thoughts
This problem is a classic example of binary search applied to array patterns rather than sorted values.
Understanding the slope comparison technique used here will help solve other problems such as:
- Find Peak Element
- Mountain Array Search
- Bitonic Array Problems
Mastering these patterns significantly improves binary search problem-solving skills for coding interviews.




