75-days-dsa-challenge

Ninja technique🥷 to ACE DSA Interviews.

View the Project on GitHub

162. Find Peak Element

class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        int n = nums.size();
        for(int i = 0 ; i< n-1;i++){
            if(nums[i]>nums[i+1])return i;
        }
        return n - 1;
    }
};

Code

class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        int n = nums.size();
        int l = 0, r = n - 1;
        while(l<r){
            int mid = l + (r - l) / 2;
            if(nums[mid]<nums[mid+1]){
                l = mid + 1;
            }else{
                r = mid;
            }
        }
        return l;
    }
};