package array.iterative;
public class PeakIndexInAMountainArray {
public int peakIndexInMountainArray(int[] nums) {
int leftPtr = 0;
int rightPtr = nums.length - 1;
int peakPtr = -1;
while (leftPtr <= rightPtr) {
int midPtr = leftPtr + (rightPtr - leftPtr) / 2;
if (nums[midPtr] > nums[midPtr + 1]) {
peakPtr = midPtr;
rightPtr = midPtr - 1;
} else
leftPtr = midPtr + 1;
}
return peakPtr;
}
}
#include <vector>
class Solution {
public:
constexpr int peakIndexInMountainArray(std::vector<int>& arr) const noexcept {
const int n = static_cast<int>(arr.size() - 1);
int left = 0, right = n - 1;
while (left < right) {
const int mid = left + (right - left) / 2;
if (arr[mid] > arr[mid + 1])
right = mid;
else
left = mid + 1;
}
return right;
}
};