forked from super30admin/Binary-Search-2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPeakElement.java
More file actions
31 lines (25 loc) · 756 Bytes
/
Copy pathFindPeakElement.java
File metadata and controls
31 lines (25 loc) · 756 Bytes
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
/*
Time Complexity : O(log(N)) N is size of input
Space Complexity :O(1)
Leetcode accepted : YES
*/
class Solution {
public int findPeakElement(int[] nums) {
if (nums == null && nums.length == 0){
return -1;
}
int left = 0;
int right = nums.length - 1;
while(left < right){
int mid = (left + right)/2;
// if mid element is greated than mid +1 we will update right pointer to mid
// else left pointer to mid + 1
if(nums[mid] > nums[mid+1]){
right = mid;
}else{
left = mid + 1;
}
}
return left;
}
}