Similar Problems

Similar Problems not available

Find In Mountain Array - Leetcode Solution

Companies:

LeetCode:  Find In Mountain Array Leetcode Solution

Difficulty: Hard

Topics: binary-search array  

Problem Statement:

You are given an integer array nums sorted in ascending order, and an integer target.

Suppose that nums is rotated at some pivot unknown to you beforehand (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

If target is found in the array return its index, otherwise, return -1.

Example 1:

Input: nums = [1,3,5,6], target = 5 Output: 2

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4

Example 3:

Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1

Solution:

The problem can be solved using the binary search algorithm. The approach is broken down into two parts:

  1. Find the pivot point where the array is rotated

  2. Do a binary search on the two halves of the array

  3. Find the Pivot Point:

To find the pivot point, we can use a binary search approach. We can start by setting the left pointer to 0 and the right pointer to the last element in the array. We can then find the mid element by calculating (left+right)/2.

If the mid element is greater than the right element, then the pivot point must be on the right half of the array. We can move the left pointer to mid+1.

If the mid element is less than the right element, then the pivot point must be on the left half of the array. We can move the right pointer to mid.

We repeat this process until the left pointer is less than or equal to the right pointer. At this point, the left pointer will be pointing to the pivot element in the array.

  1. Binary search on the two halves:

Once we have found the pivot point, we can divide the array into two parts – the left half and the right half. We can then do a binary search on each of these two halves to find the target element.

To do a binary search, we can set the left pointer to 0 and the right pointer to the last element in the array. We can then find the mid element by calculating (left+right)/2.

If the mid element is equal to the target element, we can return the mid index.

If the mid element is less than the target element, we can move the left pointer to mid+1.

If the mid element is greater than the target element, we can move the right pointer to mid-1.

We repeat this process until the left pointer is greater than or equal to the right pointer. At this point, if we have not found the target element, we can return -1.

Code:

Here is the Python code for the solution:

class Solution:
    def findInMountainArray(self, target: int, mountain_arr: 'MountainArray') -> int:
        
        # Step 1: Find the pivot point
        left, right = 0, mountain_arr.length() - 1
        
        while left < right:
            mid = (left + right) // 2
            
            if mountain_arr.get(mid) < mountain_arr.get(mid + 1):
                left = mid + 1
            else:
                right = mid
        
        # Step 2: Do a binary search on the two halves
        pivot = left
        left, right = 0, pivot
        
        while left <= right:
            mid = (left + right) // 2
            
            if mountain_arr.get(mid) == target:
                return mid
            elif mountain_arr.get(mid) < target:
                left = mid + 1
            else:
                right = mid - 1
        
        left, right = pivot, mountain_arr.length() - 1
        
        while left <= right:
            mid = (left + right) // 2
            
            if mountain_arr.get(mid) == target:
                return mid
            elif mountain_arr.get(mid) < target:
                right = mid - 1
            else:
                left = mid + 1
        
        return -1

Time complexity:

The time complexity of this solution is O(log n), where n is the length of the array.

Find In Mountain Array Solution Code

1