Find in Mountain Array
HardUpdated: Jul 31, 2025
Practice on:
Problem
You may recall that an array arr is a mountain array if and only if:
arr.length >= 3- There exists some
iwith0 < i < arr.length - 1such 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.
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 indexk(0-indexed).MountainArray.length()returns the length of the array.
Submissions making more than 100 calls to MountainArray.get will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.
Examples
Example 1:
Input:
array = [1,2,3,4,5,3,1], target = 3
Output:
2
Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.
Example 2:
Input:
array = [0,1,2,4,2,1], target = 3
Output:
-1
Explanation: 3 does not exist in `the array,` so we return -1.
Solution
Method 1 – Binary Search for Peak and Target
Intuition
A mountain array increases to a peak and then decreases. We can use binary search to find the peak, then binary search for the target in both the increasing and decreasing parts.
Approach
- Use binary search to find the peak index (where arr[i] > arr[i+1]).
- Binary search for the target in the increasing part (left of peak).
- If not found, binary search for the target in the decreasing part (right of peak).
- Return the minimum index found, or -1 if not found.
Code
C++
class Solution {
public:
int findInMountainArray(int target, MountainArray &arr) {
int n = arr.length();
int l = 0, r = n - 1;
while (l < r) {
int m = l + (r - l) / 2;
if (arr.get(m) < arr.get(m + 1)) l = m + 1;
else r = m;
}
int peak = l;
l = 0, r = peak;
while (l <= r) {
int m = l + (r - l) / 2;
int v = arr.get(m);
if (v == target) return m;
if (v < target) l = m + 1;
else r = m - 1;
}
l = peak + 1, r = n - 1;
while (l <= r) {
int m = l + (r - l) / 2;
int v = arr.get(m);
if (v == target) return m;
if (v > target) l = m + 1;
else r = m - 1;
}
return -1;
}
};
Go
type MountainArray interface {
Get(index int) int
Length() int
}
func findInMountainArray(target int, arr MountainArray) int {
n := arr.Length()
l, r := 0, n-1
for l < r {
m := l + (r-l)/2
if arr.Get(m) < arr.Get(m+1) {
l = m + 1
} else {
r = m
}
}
peak := l
l, r = 0, peak
for l <= r {
m := l + (r-l)/2
v := arr.Get(m)
if v == target {
return m
}
if v < target {
l = m + 1
} else {
r = m - 1
}
}
l, r = peak+1, n-1
for l <= r {
m := l + (r-l)/2
v := arr.Get(m)
if v == target {
return m
}
if v > target {
l = m + 1
} else {
r = m - 1
}
}
return -1
}
Java
class Solution {
public int findInMountainArray(int target, MountainArray arr) {
int n = arr.length();
// Find peak
int l = 0, r = n - 1;
while (l < r) {
int m = l + (r - l) / 2;
if (arr.get(m) < arr.get(m + 1)) l = m + 1;
else r = m;
}
int peak = l;
// Search in increasing part
l = 0; r = peak;
while (l <= r) {
int m = l + (r - l) / 2;
int v = arr.get(m);
if (v == target) return m;
if (v < target) l = m + 1;
else r = m - 1;
}
// Search in decreasing part
l = peak + 1; r = n - 1;
while (l <= r) {
int m = l + (r - l) / 2;
int v = arr.get(m);
if (v == target) return m;
if (v > target) l = m + 1;
else r = m - 1;
}
return -1;
}
}
Kotlin
interface MountainArray {
fun get(index: Int): Int
fun length(): Int
}
class Solution {
fun findInMountainArray(target: Int, arr: MountainArray): Int {
val n = arr.length()
var l = 0
var r = n - 1
while (l < r) {
val m = l + (r - l) / 2
if (arr.get(m) < arr.get(m + 1)) l = m + 1
else r = m
}
val peak = l
l = 0; r = peak
while (l <= r) {
val m = l + (r - l) / 2
val v = arr.get(m)
if (v == target) return m
if (v < target) l = m + 1
else r = m - 1
}
l = peak + 1; r = n - 1
while (l <= r) {
val m = l + (r - l) / 2
val v = arr.get(m)
if (v == target) return m
if (v > target) l = m + 1
else r = m - 1
}
return -1
}
}
Python
class MountainArray:
def get(self, index: int) -> int: ...
def length(self) -> int: ...
class Solution:
def findInMountainArray(self, target: int, arr: MountainArray) -> int:
n = arr.length()
l, r = 0, n - 1
while l < r:
m = (l + r) // 2
if arr.get(m) < arr.get(m + 1):
l = m + 1
else:
r = m
peak = l
l, r = 0, peak
while l <= r:
m = (l + r) // 2
v = arr.get(m)
if v == target:
return m
if v < target:
l = m + 1
else:
r = m - 1
l, r = peak + 1, n - 1
while l <= r:
m = (l + r) // 2
v = arr.get(m)
if v == target:
return m
if v > target:
l = m + 1
else:
r = m - 1
return -1
Rust
trait MountainArray {
fn get(&self, index: i32) -> i32;
fn length(&self) -> i32;
}
impl Solution {
pub fn find_in_mountain_array(target: i32, arr: &dyn MountainArray) -> i32 {
let n = arr.length();
let (mut l, mut r) = (0, n - 1);
while l < r {
let m = l + (r - l) / 2;
if arr.get(m) < arr.get(m + 1) {
l = m + 1;
} else {
r = m;
}
}
let peak = l;
l = 0; r = peak;
while l <= r {
let m = l + (r - l) / 2;
let v = arr.get(m);
if v == target {
return m;
}
if v < target {
l = m + 1;
} else {
r = m - 1;
}
}
l = peak + 1; r = n - 1;
while l <= r {
let m = l + (r - l) / 2;
let v = arr.get(m);
if v == target {
return m;
}
if v > target {
l = m + 1;
} else {
r = m - 1;
}
}
-1
}
}
TypeScript
interface MountainArray {
get(index: number): number;
length(): number;
}
class Solution {
findInMountainArray(target: number, arr: MountainArray): number {
const n = arr.length();
let l = 0, r = n - 1;
while (l < r) {
const m = Math.floor((l + r) / 2);
if (arr.get(m) < arr.get(m + 1)) l = m + 1;
else r = m;
}
const peak = l;
l = 0; r = peak;
while (l <= r) {
const m = Math.floor((l + r) / 2);
const v = arr.get(m);
if (v === target) return m;
if (v < target) l = m + 1;
else r = m - 1;
}
l = peak + 1; r = n - 1;
while (l <= r) {
const m = Math.floor((l + r) / 2);
const v = arr.get(m);
if (v === target) return m;
if (v > target) l = m + 1;
else r = m - 1;
}
return -1;
}
}
Complexity
- ⏰ Time complexity:
O(log n), each binary search is O(log n). - 🧺 Space complexity:
O(1), only constant extra space is used.