From 126aab53c4187782f16b270e68c0af976cdc02e2 Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Tue, 30 Dec 2025 19:48:43 -0600 Subject: [PATCH 1/3] Find peak element js code --- findPeakElement.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 findPeakElement.js diff --git a/findPeakElement.js b/findPeakElement.js new file mode 100644 index 00000000..3c876509 --- /dev/null +++ b/findPeakElement.js @@ -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 usually use the condition left<=right in the while loop, but this scenario i removed the equal to condition as return left covers it. + I am moving my pointers towards the peak always, if the peak resides on the left iam moving my right pointer towards left + otherwise left pointer to the right. + + */ +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 - 1; + } + } + return left; +}; + From b7c42a86abd77db385ca9bfd05c103e451be2a0b Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Tue, 30 Dec 2025 19:48:57 -0600 Subject: [PATCH 2/3] comments updated --- findPeakElement.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/findPeakElement.js b/findPeakElement.js index 3c876509..e540b452 100644 --- a/findPeakElement.js +++ b/findPeakElement.js @@ -4,7 +4,8 @@ I usually use the condition left<=right in the while loop, but this scenario i removed the equal to condition as return left covers it. I am moving my pointers towards the peak always, if the peak resides on the left iam moving my right pointer towards left 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; From e7cb58ebc91ba7434b1ea90cc7530af63b261cda Mon Sep 17 00:00:00 2001 From: RamKuppagiri Date: Tue, 30 Dec 2025 20:17:38 -0600 Subject: [PATCH 3/3] updated as per the suggestions --- findPeakElement.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/findPeakElement.js b/findPeakElement.js index e540b452..6690ded6 100644 --- a/findPeakElement.js +++ b/findPeakElement.js @@ -1,8 +1,7 @@ /** * Intution is to use binary search as the requirement is to write an algorithm which completes in O(log n) - I usually use the condition left<=right in the while loop, but this scenario i removed the equal to condition as return left covers it. - I am moving my pointers towards the peak always, if the peak resides on the left iam moving my right pointer towards left + 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. @@ -14,7 +13,7 @@ var findPeakElement = function (nums) { if (nums[mid] < nums[mid + 1]) { left = mid + 1; } else { - right = mid - 1; + right = mid; } } return left;