Peak element | Practice | GeeksforGeeks

An element is called a peak element if its value is not smaller than the value of its adjacent elements(if they exists).
Given an array arr[] of size N, find the index of any one of its peak elements.
Note: The generated output will always be 1 if the index that you return is correct. Otherwise output will be 0. 

Example 1:
Input:
N = 3
arr[] = {1,2,3}
Output: 2
Explanation: index 2 is 3.
It is the peak element as it is 
greater than its neighbour 2.

Example 2:
Input:
N = 2
arr[] = {3,4}
Output: 1
Explanation: 4 (at index 1) is the 
peak element as it is greater than 
its only neighbour element 3.

 
Your Task:
You don't have to read input or print anything. Complete the function peakElement() which takes the array arr[] and its size N as input parameters and return the index of any one of its peak elements.

Can you solve the problem in expected time complexity?
 
Expected Time Complexity: O(log N)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ N ≤ 105
1 ≤ A[] ≤ 106
-2
kashyapjhon17 hours ago
C++ Solution Time=(0.25/1.48) EASY:
int peakElement(int arr[], int n)   {      // Your code here      if(n==1){          return 0;      }      else{          for(int i=0;i<n;i++){              if(i==0 && arr[i]>arr[i+1]){                  return i;              }              if(i>0 && i<n-1 && arr[i]>arr[i-1] && arr[i]>arr[i+1]){                  return i;              }              if(i==n-1 && arr[i]>arr[n-2]){                  return i;              }          }          return 0;      }   }
0
keith91911 day ago
public int peakElement(int[] arr,int n)   {      //add code here.      int pointer = 0;            while(pointer < n){          //boolean isPeak = false;          int left = pointer - 1 > 0 ? arr[pointer -1] : Integer.MIN_VALUE;          int right = pointer + 1 < n ? arr[pointer + 1] :  Integer.MIN_VALUE;                 if(arr[pointer] > left && arr[pointer] > right)          {              return pointer;          }                    pointer++;                }            return 0;   }
0
kalaiabster2 days ago
JAVA SOLUTION Time Taken(1.51/3.86)
public int peakElement(int[] arr,int n)   {       int out=0;      for(int i=0;i<arr.length-1;i++)      {          if(arr[i]<arr[i+1])          {              out=i+1;          }      }      return out;   }
 
+1
aradgast12 days ago
how can i solve it in O(logN)? unless the array is sorted and then it's clear...
0
sahithichowdhary93 days ago
class Solution{// Function to find the peak element// arr[]: input array// n: size of array a[]public int peakElement(int[] arr,int n)   {      //add code here.      int j=0;      HashMap<Integer,Integer>map=new HashMap<>();      for(int i=0;i<n-1;i++)      {          if(arr[i+1]>arr[i])          {                j=i+1;              map.put(j,i+1);          }      }   map.get(j);    return j;   }
}
0
vishal8579943 days ago
///O(n) in java
 public int peakElement(int[] arr,int n)   {      //add code here.      int low=0;int high=n-1;      while(low<=high){          int mid=high-(high-low)/2;          if((mid==0 || arr[mid]>=arr[mid-1])&&(mid==n-1 ||arr[mid]>=arr[mid+1])){              return mid;          }          if(mid>0 && arr[mid-1]>=arr[mid]){            high=mid-1;          }else{           low=mid+1;          }      }      return -1;         }
+1
riteshthakare11913 days ago
For begineer:
int peakElement(int arr[], int n)   {       if (arr[0]>arr[1]){           return 0;       }       if (arr[n-1]>arr[n-2]){           return n-1;       }              for (int i=1;i<=(n-2);i++){           if(arr[i]>arr[i-1] && arr[i]>arr[i+1]){               return i;           }       }              return 0;      // Your code here   }
 
0
mankesh0163 days ago
O(log N) solution
public:
    int findPeak(int arr[],int st, int end){
        if(st==end) return st;
        
        int mid=(st+end)/2;
        
        if(arr[mid]<arr[mid+1]) return findPeak(arr,mid+1,end);
        else if(arr[mid]<arr[mid-1]) return findPeak(arr,st,mid-1);
        else return mid;
    }
    int peakElement(int arr[], int n)
    {
       // Your code here
       
       return findPeak(arr,0,n-1);
    }
 
0
mankesh016
This comment was deleted.
0
mankesh0163 days ago
O(logN) solution c++
 
int findPeak( int arr[ ], int st, int end){
    if(st==end) return st ;
   int mid=(st+end)/2;
   if        (arr[mid]<arr[mid+1]) 
                     return findPeak(arr,mid+1,end);
   else if   (arr[mid]<arr[mid-1]) 
                     return findPeak(arr, st, mid-1);
   else            return mid;
}
int peakElement(int arr[], int n){
       // Your code here
       return findPeak(arr,0,n-1);
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.