Peak Index in a Mountain Array

styluso7

Stylus07

Posted on December 3, 2021

Peak Index in a Mountain Array

Problem

Let's call an array arr a mountain if the following properties hold:
arr.length >= 3

There exists some i with 0 < i < arr.length - 1 such that:
arr[0] < arr[1] < ... arr[i-1] < arr[i]
arr[i] > arr[i+1] > ... > arr[arr.length - 1]
Given an integer array arr that is guaranteed to be a mountain, return any i such that arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1].

Brute Force Approach

var peakIndexInMountainArray = function(arr) {
    let count =0;

    for(let start=0; start < arr.length; start++){
        if(arr[start] < arr[start+1]){
            count+=1;
        }
    }
    return count;
};
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
styluso7
Stylus07

Posted on December 3, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Peak Index in a Mountain Array
leetcode Peak Index in a Mountain Array

December 3, 2021