-
Notifications
You must be signed in to change notification settings - Fork 5
/
FindPeakElement.java
36 lines (29 loc) · 1 KB
/
FindPeakElement.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
Problem Statement:
A peak element is an element that is greater than its neighbors.
Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that nums[-1] = nums[n] = -∞.
Problem Link:
Find Peak Element: https://leetcode.com/problems/find-peak-element/description/
Solution:
https://github.com/sunnypatel165/leetcode-again/blob/master/solutions/BalancedBinaryTree.java
Author:
Sunny Patel
https://github.com/sunnypatel165
https://www.linkedin.com/in/sunnypatel165/
*/
class Solution {
public int findPeakElement(int[] nums) {
int left=0, right = nums.length-1;
while(left < right){
int mid = (left + right)/2;
if(nums[mid] > nums[mid+1])
right = mid;
else
left = mid+1;
}
return left;
}
}