(This problem is an interactive problem.)

You may recall that an array arr is a mountain array if and only if:


	arr.length &gt;= 3
	There exists some i with 0 &lt; i &lt; arr.length - 1 such that:
	
		arr[0] &lt; arr[1] &lt; ... &lt; arr[i - 1] &lt; arr[i]
		arr[i] &gt; arr[i + 1] &gt; ... &gt; 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.

&nbsp;
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.


&nbsp;
Constraints:


	3 &lt;= mountain_arr.length() &lt;= 104
	0 &lt;= target &lt;= 109
	0 &lt;= mountain_arr.get(index) &lt;= 109


# """
# This is MountainArray's API interface.
# You should not implement it, or speculate about its implementation
# """
#class MountainArray:
#    def get(self, index: int) -> int:
#    def length(self) -> int:

class Solution:
    def findInMountainArray(self, target: int, mountain_arr: 'MountainArray') -> int:
        min_index = -1
        peak = self.findpeak(mountain_arr)
        
        min_index = self.binary_search(0, peak, mountain_arr, target, 1)
        if min_index == -1:
            min_index = self.binary_search(peak+1, mountain_arr.length() - 1, mountain_arr, target, -1)
        return min_index

    def findpeak(self, mountain_arr):
        start = 0
        end = mountain_arr.length() - 1
        while start < end:
            mid = start + int((end - start)/2)
            if mountain_arr.get(mid) < mountain_arr.get(mid + 1):
                start = mid + 1
            else:
                end = mid
        
        return start
    
    def binary_search(self, start, end, mountain_arr, target, asc):
        while start <= end:
            mid = start + int((end - start)/2)
            mountain_arr_get_mid = mountain_arr.get(mid)
            if target == mountain_arr_get_mid:
                return mid
            if asc == 1:
                if target < mountain_arr_get_mid:
                    end = mid - 1
                elif target > mountain_arr_get_mid:
                    start = mid + 1
            else:
                if target < mountain_arr_get_mid:
                    start = mid + 1
                elif target > mountain_arr_get_mid:
                    end = mid - 1
            
        return -1
class Solution {
    public int findInMountainArray(int target, MountainArray mountainArr) {
        int peak = findPeak(mountainArr);
        
        int left =binary(0,peak,mountainArr,target,true);
            if(left!=-1){
                return left;
            }
        int right= binary(peak+1,mountainArr.length()-1,mountainArr, target,false);
        return right;  
    }
    
    static int findPeak(MountainArray mountainArr){
        int start=0;
        int end =mountainArr.length()-1;
        
        while(start<end){
            int mid=start+((end-start)/2);
            
            if(mountainArr.get(mid)<mountainArr.get(mid+1)){
                start=mid+1;
            }else{
                end=mid;
            }
        }
        return start;
    }
    
    static int binary(int low, int high, MountainArray arr,int target, boolean left){
            while(low<=high){
                int mid=low+((high-low)/2);
                if(target<arr.get(mid)){
                    if(left){
                        high=mid-1;
                    }else{
                         low=mid+1;
                    }   
                }else if(target>arr.get(mid)){
                    if(left){
                        low=mid+1;
                    }else{
                       high=mid-1; 
                    }    
                }else{
                    return mid;
                }
            } 
        return -1;
    }
}
/**
 * // This is the MountainArray's API interface.
 * // You should not implement it, or speculate about its implementation
 * class MountainArray {
 * public:
 * int get(int index);
 * int length();
 * };
 */

class Solution {
public:

    //----------------------- find peak ---------------------------------------

    int find_max(MountainArray &mountainArr,int start,int end)
    { int n= mountainArr.length();
        while(start<=end)
        { int mid= start+(end-start)/2;
           int next=(mid+1)%n;
          int prev=(mid+n-1)%n;

         int mid_val = mountainArr.get(mid);
         int pre_val = mountainArr.get(prev);
         int next_val = mountainArr.get(next);

           if(mid_val > next_val and mid_val>pre_val)
               return mid;

           else if(mid_val<next_val and mid_val > pre_val)
                start=mid+1;

           else if(mid_val>next_val and mid_val<pre_val)
                end=mid-1;

        }
        return -1;

    }

   //--------------------------binary search-------------------------------------------

   int binary_search(MountainArray &mountainArr,int start,int end,int target)
   {
       while(start<=end)
       {
           int mid= start + (end-start)/2;
             int mid_val=mountainArr.get(mid);

           if(mid_val==target)
               return mid;
          else if(target < mid_val)
           {
               end=mid-1;
           }
           else
               start=mid+1;
       }
       return -1;
   }

    //----------------------binary search in reverse sorted------------------------------

       int binary_search_rev(MountainArray &mountainArr,int start,int end,int target)
   {
       while(start<=end)
       {
           int mid= start + (end-start)/2;
           int mid_val=mountainArr.get(mid);
           if(mid_val==target)
               return mid;
          else if(target > mid_val)
           {
               end=mid-1;
           }
           else
               start=mid+1;
       }
       return -1;
   }

  //------------------------------returns minimum index of target--------------------------------------

    int evaluate_ans(int a,int b)
    {
        if(a==-1 and b==-1 )
            return -1;
        else if(a!= -1 and b!= -1)
            return min(a,b);
        else if(a==-1 and b!=-1)
            return b;
        else
            return a;

    }

    int findInMountainArray(int target, MountainArray &mountainArr) {

        int start=0;
        int n= mountainArr.length()-1;
        int max_in = find_max(mountainArr,start ,n);

        int a= binary_search(mountainArr,start,max_in,target);
        int b= binary_search_rev(mountainArr,max_in + 1,n,target);

        return evaluate_ans(a,b);
    }
};
/**
 * // This is the MountainArray's API interface.
 * // You should not implement it, or speculate about its implementation
 * function MountainArray() {
 *     @param {number} index
 *     @return {number}
 *     this.get = function(index) {
 *         ...
 *     };
 *
 *     @return {number}
 *     this.length = function() {
 *         ...
 *     };
 * };
 */

var binarySearch = function(left, right, condition) {
    while (left < right) {
        var mid = left + Math.floor((right - left) / 2);
        if (condition(mid)) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return left;
}

/**
 * @param {number} target
 * @param {MountainArray} mountainArr
 * @return {number}
 */
var findInMountainArray = function(target, mountainArr) {
    var n = mountainArr.length();
    var maxNumIdx = binarySearch(1, n - 2, function(idx) {
        return mountainArr.get(idx) > mountainArr.get(idx + 1);
    });
    var leftIdx = binarySearch(0, maxNumIdx, function(idx) {
        return mountainArr.get(idx) >= target;
    });
    if (mountainArr.get(leftIdx) === target) {
        return leftIdx;
    }
    var rightIdx = binarySearch(maxNumIdx, n - 1, function(idx) {
        return mountainArr.get(idx) <= target;
    });
    if (mountainArr.get(rightIdx) === target) {
        return rightIdx;
    }
    return -1;
};
Find in Mountain Array