LeetCode 1095: Find in Mountain Array Solution

Master LeetCode problem 1095 (Find in Mountain Array), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1095. Find in Mountain Array

Problem Explanation

Explanation

To solve this problem, we can use a binary search approach to find the peak element in the mountain array. Once we find the peak element, we can perform two separate binary searches on the increasing and decreasing parts of the array to find the target.

  1. Find the peak element in the mountain array using binary search.
  2. Perform binary search on the increasing part of the array for the target.
  3. If the target is not found in the increasing part, perform binary search on the decreasing part of the array for the target.

The time complexity of this approach is O(log n), where n is the length of the mountain array. The space complexity is O(1) as we are not using any extra space apart from a few variables.

Solution Code

class Solution {
    public int findInMountainArray(int target, MountainArray mountainArr) {
        int peakIndex = findPeakIndex(mountainArr);
        
        int leftResult = binarySearch(mountainArr, target, 0, peakIndex, true);
        if (leftResult != -1) {
            return leftResult;
        }
        
        return binarySearch(mountainArr, target, peakIndex + 1, mountainArr.length() - 1, false);
    }
    
    private int findPeakIndex(MountainArray mountainArr) {
        int left = 0, right = mountainArr.length() - 1;
        
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (mountainArr.get(mid) < mountainArr.get(mid + 1)) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        
        return left;
    }
    
    private int binarySearch(MountainArray mountainArr, int target, int left, int right, boolean increasing) {
        while (left <= right) {
            int mid = left + (right - left) / 2;
            int midElement = mountainArr.get(mid);
            
            if (midElement == target) {
                return mid;
            } else if ((midElement < target && increasing) || (midElement > target && !increasing)) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        
        return -1;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1095 (Find in Mountain Array)?

This page provides optimized solutions for LeetCode problem 1095 (Find in Mountain Array) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1095 (Find in Mountain Array)?

The time complexity for LeetCode 1095 (Find in Mountain Array) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1095 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1095 in Java, C++, or Python.

Back to LeetCode Solutions