Find in Mountain Array - Problem

You may recall that an array arr is a mountain array if and only if:

  • arr.length >= 3
  • There exists some i with 0 < i < arr.length - 1 such that:
    • arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
    • arr[i] > arr[i + 1] > ... > arr[arr.length - 1]

Given a mountain array mountainArr, return the minimum index such that mountainArr.get(index) == target. If such an index does not exist, return -1.

Important: You cannot access the mountain array directly. You may only access the array using a MountainArray interface:

  • MountainArray.get(k) returns the element of the array at index k (0-indexed).
  • MountainArray.length() returns the length of the array.

Submissions making more than 100 calls to MountainArray.get will be judged Wrong Answer.

Input & Output

Example 1 — Basic Mountain Search
$ Input: mountain_arr = [1,2,3,4,5,3,1], target = 3
Output: 2
💡 Note: The mountain array is [1,2,3,4,5,3,1] with peak at index 4 (value 5). Target 3 appears at indices 2 and 5, so return the minimum index 2.
Example 2 — Target Not Found
$ Input: mountain_arr = [0,1,2,4,2,1], target = 3
Output: -1
💡 Note: The mountain array is [0,1,2,4,2,1] with peak at index 3 (value 4). Target 3 does not exist in the array, so return -1.
Example 3 — Target at Peak
$ Input: mountain_arr = [1,2,0,1,0], target = 0
Output: 2
💡 Note: The mountain array is [1,2,0,1,0] with peak at index 1 (value 2). Target 0 appears at indices 2 and 4, so return the minimum index 2.

Constraints

  • 3 ≤ mountain_arr.length() ≤ 104
  • 0 ≤ target ≤ 109
  • mountain_arr is guaranteed to be a mountain array

Visualization

Tap to expand
Find in Mountain Array INPUT Mountain Array Visualization 1 2 3 4 Peak 5 3 1 Indices: 0 1 2 3 4 5 6 [1, 2, 3, 4, 5, 3, 1] Target = 3 Find min index where val=3 ALGORITHM STEPS 1 Find Peak Index Binary search: if mid < mid+1 peak is right, else left 2 Search Left Side Binary search [0, peak] Ascending order 3 Search Right Side If not found left, search [peak+1, end] descending 4 Return Result Return left result if found else right result or -1 Binary Search Flow Peak found at index 4 (val=5) Left search [0-4]: found 3 at i=2 OK - Return index 2 FINAL RESULT Minimum Index Found 1 0 2 1 3 2 4 3 5 4 3 5 1 6 First occurrence (min index) Second occurrence (ignored) Output 2 OK - Index 2 is minimum arr[2] = 3 = target Complexity Time: O(log n) Space: O(1) Key Insight: Use THREE binary searches: (1) Find peak to split array into ascending and descending parts. (2) Search ascending left side first for minimum index. (3) Only search descending right side if needed. This ensures O(log n) time and stays within 100 API calls limit. Left result is always smaller index. TutorialsPoint - Find in Mountain Array | Optimal Solution (Triple Binary Search)
Asked in
Google 45 Facebook 38 Amazon 32 Apple 25
125.0K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen