Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions findPeakElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
*
Intution is to use binary search as the requirement is to write an algorithm which completes in O(log n)
I am moving my pointers towards the peak always, if the peak resides on the left iam moving my right pointer towards mid
otherwise left pointer to the right.
Time complexity : O(log n)
Space Complexity: O(1) as i am not using any extra space other than left and right pointers.
*/
var findPeakElement = function (nums) {
let left = 0, right = nums.length - 1;
while(left < right) {
const mid = Math.floor(left + (right - left) / 2);
if (nums[mid] < nums[mid + 1]) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
};