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.

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. Also, any solutions that attempt to circumvent the judge will result in disqualification.

Examples

Example 1:

1
2
3
4
5
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:

1
2
3
4
5
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

  1. Use binary search to find the peak index (where arr[i] > arr[i+1]).
  2. Binary search for the target in the increasing part (left of peak).
  3. If not found, binary search for the target in the decreasing part (right of peak).
  4. Return the minimum index found, or -1 if not found.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.