Leetcode 882 Solution

This article provides solution to leetcode question 882 (peak-index-in-a-mountain-array)

https://leetcode.com/problems/peak-index-in-a-mountain-array

Solution

class Solution {
public:
    int peakIndexInMountainArray(vector<int>& A) {
        int l = 0;
        int r = A.size() - 1;

        while (l < r)
        {
            int m = (l + r) / 2;

            if (A[m - 1] < A[m] && A[m] > A[m + 1])
                return m;

            if (A[m - 1] < A[m] && A[m] < A[m + 1])
                l = m;
            else if (A[m - 1] > A[m] && A[m] > A[m + 1])
                r = m;
        }

        return 0;
    }
};