From 5020e65ee8a1d216bfd33e1f9a787bae9d4444e8 Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Thu, 22 Jun 2023 18:59:40 +0530 Subject: [PATCH 1/3] Create 0034-find-first-and-last-position-of-element-in-sorted-array.js Solved find-first-and-last-position-of-element-in-sorted-array in js. --- ...ast-position-of-element-in-sorted-array.js | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 javascript/0034-find-first-and-last-position-of-element-in-sorted-array.js diff --git a/javascript/0034-find-first-and-last-position-of-element-in-sorted-array.js b/javascript/0034-find-first-and-last-position-of-element-in-sorted-array.js new file mode 100644 index 000000000..da545a194 --- /dev/null +++ b/javascript/0034-find-first-and-last-position-of-element-in-sorted-array.js @@ -0,0 +1,47 @@ +/** + * Binary Search + * https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ + * Time O(log(n)) | Space O(1) + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var searchRange = function(nums, target) { + + const result = []; + + result.push(binarySearch(true)); + result.push(binarySearch(false)); + + function binarySearch(leftBias) { + let left = 0; + let right = nums.length - 1; + let index = -1; + + while(left <= right) { + + const mid = Math.floor((left+right)/2); + + if(target > nums[mid]) { + left = mid+1; + } + if(target < nums[mid]) { + right = mid-1; + } + // this is the meat of the code + if(target === nums[mid]) { + if(leftBias) { + index = mid; + right = mid - 1; + } else { + index = mid; + left = mid + 1; + } + } + } + + return index; + } + + return result; +}; From b44ad1666937ca3a6e1acc53c46c4dd8fd109d47 Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Mon, 3 Jul 2023 20:19:10 +0530 Subject: [PATCH 2/3] Update 0034-find-first-and-last-position-of-element-in-sorted-array.js Making variable names more descriptive. --- ...first-and-last-position-of-element-in-sorted-array.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/javascript/0034-find-first-and-last-position-of-element-in-sorted-array.js b/javascript/0034-find-first-and-last-position-of-element-in-sorted-array.js index da545a194..98b6bf729 100644 --- a/javascript/0034-find-first-and-last-position-of-element-in-sorted-array.js +++ b/javascript/0034-find-first-and-last-position-of-element-in-sorted-array.js @@ -13,7 +13,7 @@ var searchRange = function(nums, target) { result.push(binarySearch(true)); result.push(binarySearch(false)); - function binarySearch(leftBias) { + function binarySearch(isLeftBias) { let left = 0; let right = nums.length - 1; let index = -1; @@ -28,9 +28,10 @@ var searchRange = function(nums, target) { if(target < nums[mid]) { right = mid-1; } - // this is the meat of the code - if(target === nums[mid]) { - if(leftBias) { + + const isTarget = target === nums[mid]; + if(isTarget) { + if(isLeftBias) { index = mid; right = mid - 1; } else { From fe9c7060890c04249e64d6f6bd2a4d8289146d5c Mon Sep 17 00:00:00 2001 From: Aadil <77232799+aadil42@users.noreply.github.com> Date: Sat, 22 Jul 2023 16:25:04 +0530 Subject: [PATCH 3/3] Update 0034-find-first-and-last-position-of-element-in-sorted-array.js Using the bitwise operator and removing the inner function. --- ...-last-position-of-element-in-sorted-array.js | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/javascript/0034-find-first-and-last-position-of-element-in-sorted-array.js b/javascript/0034-find-first-and-last-position-of-element-in-sorted-array.js index 98b6bf729..c11490e11 100644 --- a/javascript/0034-find-first-and-last-position-of-element-in-sorted-array.js +++ b/javascript/0034-find-first-and-last-position-of-element-in-sorted-array.js @@ -10,17 +10,20 @@ var searchRange = function(nums, target) { const result = []; - result.push(binarySearch(true)); - result.push(binarySearch(false)); + result.push(binarySearch(true, nums, target)); + result.push(binarySearch(false, nums, target)); - function binarySearch(isLeftBias) { + return result; +}; + +var binarySearch = (isLeftBias, nums, target) => { let left = 0; let right = nums.length - 1; let index = -1; while(left <= right) { - const mid = Math.floor((left+right)/2); + const mid = (left + right) >> 1; if(target > nums[mid]) { left = mid+1; @@ -40,9 +43,5 @@ var searchRange = function(nums, target) { } } } - return index; - } - - return result; -}; +}