AlgoMaster Logo

Find in Mountain Array

hard6 min readUpdated June 23, 2026

Understanding the Problem

This problem combines mountain arrays and binary search. A mountain array rises strictly to a peak, then falls strictly from that peak. We are given a target value and need to find the smallest index where it appears. The constraint is that we cannot scan the array directly. We can only access elements through an API, and we are limited to 100 calls total.

A mountain array is two sorted arrays joined at the peak. The left half is sorted in ascending order and the right half is sorted in descending order. Once we locate the peak, we can binary search each half independently. Since we want the minimum index, we search the ascending (left) half first.

Key Constraints:

  • 3 <= mountain_arr.length() <= 10^4 → The array holds up to 10,000 elements. A linear scan would use up to 10,000 API calls. With log2(10^4) ≈ 14, each binary search costs about 14 calls, so three of them stay near 42 calls.
  • At most 100 calls to MountainArray.get → This rules out any approach that touches every element and forces an O(log n) solution.
  • 0 <= mountain_arr.get(index) <= 10^9 → Values fit in a 32-bit signed integer, so no overflow concerns and no negative values to special-case.

Approach 1: Linear Scan

Intuition

Walk through the array from left to right and return the first index where the value equals the target. Scanning left to right means the first match is the minimum index by construction.

This ignores the mountain structure entirely. It serves as a baseline before we optimize, and it shows why the call limit matters.

Algorithm

  1. Get the array length n using mountainArr.length().
  2. Iterate i from 0 to n - 1.
  3. If mountainArr.get(i) == target, return i.
  4. If the loop finishes without finding the target, return -1.

Example Walkthrough

1Start scan: i=0, looking for target=3
0
1
i
1
2
2
3
3
4
4
5
5
3
6
1
1/4

Code

The linear scan uses up to 10,000 API calls, well past the 100-call limit. The next approach uses the mountain structure: find the peak with binary search, then binary search each sorted half.

Approach 2: Find Peak + Two Binary Searches

Intuition

The left side of a mountain array increases strictly and the right side decreases strictly. Locating the peak reduces the problem to two independent binary searches on sorted subarrays.

The plan has three phases: find the peak index with binary search, search the ascending half [0, peak] with standard binary search, and if the target is not found there, search the descending half [peak, n-1] with a reversed binary search.

Algorithm

  1. Get n = mountainArr.length().
  2. Find peak: Set left = 0, right = n - 1. While left < right, compute mid. If mountainArr.get(mid) < mountainArr.get(mid + 1), set left = mid + 1. Otherwise, set right = mid. When done, left is the peak index.
  3. Search ascending half: Set left = 0, right = peak. Standard binary search: if mountainArr.get(mid) < target, go right. If mountainArr.get(mid) > target, go left. If equal, return mid.
  4. Search descending half: Set left = peak, right = n - 1. Reversed binary search: if mountainArr.get(mid) > target, go right. If mountainArr.get(mid) < target, go left. If equal, return mid.
  5. If neither search finds the target, return -1.

Example Walkthrough

1Phase 1: Find Peak. left=0, right=6
0
1
left
1
2
2
3
3
4
4
5
5
3
6
1
right
search range
1/6

Code

Approach 2 is optimal in Big-O, but it can repeat API calls. Peak finding queries mountainArr.get(mid) and mountainArr.get(mid + 1), and some of those indices are queried again during the two binary searches. Caching every result removes those duplicate calls, which matters against the 100-call ceiling.

Approach 3: Optimized with Caching

Intuition

A hash map caches the result of each mountainArr.get() call. The first time an index is queried, the value is stored. Later requests for the same index read from the map instead of calling the API again.

The savings are concentrated at the boundaries between phases. The peak index and the indices near it are queried during peak finding and then again at the start of each half-search, so caching turns those repeats into free map lookups.

Algorithm

  1. Create a hash map cache to store index → value mappings.
  2. Define a helper function getVal(index) that checks the cache first, and only calls mountainArr.get(index) if the value isn't cached.
  3. Use the same three-phase approach as Approach 2 (find peak, search ascending, search descending), but replace all direct mountainArr.get() calls with getVal().

Example Walkthrough

1Phase 1: Find Peak. left=0, right=5. Cache empty.
0
0
1
1
2
2
3
4
4
2
5
1
search range
1/8

Code