Skip to content

Commit

Permalink
Update 0162-find-peak-element.java
Browse files Browse the repository at this point in the history
The java code for this problem is missing. So I added my code which passed the testcases on leetcode.

submission link: https://leetcode.com/problems/find-peak-element/submissions/1135183935/
  • Loading branch information
RakeshTirumala authored Jan 3, 2024
1 parent 41dfdb7 commit acc26fa
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions java/0162-find-peak-element.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public int findPeakElement(int[] nums) {
int l = 0, r = nums.length-1;
while(l<=r){
int m = l + (r-l)/2;
int left = (m-1==-1)?Integer.MIN_VALUE:nums[m-1];
int right = (m+1==nums.length)?Integer.MIN_VALUE:nums[m+1];
if(nums[m]>left && nums[m]>right) return m;
else if(nums[m]<left) r=m-1;
else l = m+1;
}
return 0;
}
}

0 comments on commit acc26fa

Please sign in to comment.