AlgoMaster Logo

Find in Mountain Array

Ashish

Ashish Pratap Singh

hard

Problem Description

Solve it on LeetCode

Approaches

Intuition:

In a mountain array, there exists a peak element after which elements start decreasing. For a brute force approach, one can simply iterate over the entire array to find the target. This involves two phases:

  1. Traverse from the start to the peak (increasing sequence).
  2. Traverse from the peak to the end (decreasing sequence).

Steps:

  1. Iterate through the array from the start to the peak to look for the target.
  2. If not found, iterate from the peak to the end.

Code:

Intuition:

Utilizing the properties of the mountain array, we can apply binary search methods to efficiently find the peak and then search for the target in both increasing and decreasing sequences separately.

Steps:

  1. Use a binary search to find the peak of the mountain array.
  2. Perform a binary search on the increasing sequence from the start to the peak to find the target.
  3. If the target is not found, perform a binary search on the decreasing sequence from the peak to the end.

Code: