Skip to content
Open
Show file tree
Hide file tree
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
33 changes: 33 additions & 0 deletions findMin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @param {number[]} nums
* @return {number}
*
*
* Intuition:
*
* This solution uses binary search to find the minimum element in a rotated sorted array.
* If the array is not rotated (or rotated back to its original position), the first element will be smaller than or equal to the last, so we can immediately return it.
* Otherwise, we perform binary search to locate the rotation point, which is where the minimum element lies.
* At each step, we calculate the middle pointer and check if it is smaller than its previous element;
* if so, that element is the minimum. If not, we determine which half of the array is not sorted by comparing the middle element with the rightmost element.
* If the right side is unsorted, the minimum must reside in there, so we move the left pointer forward;
* otherwise, we search the left half by moving the right pointer backward.
* This approach efficiently narrows the search space and guarantees a time complexity of O(log n).
*/
var findMin = function(nums) {
let left = 0, right = nums.length - 1;
// edge case where the element is not roatated or the element rotated to its original place
if(nums[left] <= nums[right]) return nums[left];
while(left <= right){
const mid = Math.floor((left+ (right-left)/2));
// condition to return mid
if(nums[mid] < nums[mid-1]){
return nums[mid];
}else if(nums[right] < nums[mid]){
left = mid + 1;
}else {
right = mid - 1;
}
}
return left;
};
20 changes: 20 additions & 0 deletions findPeakElement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
Intuition 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;
};

60 changes: 60 additions & 0 deletions searchRange.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*
* Intuition:
* I am using binary search to achieve the run time complexity O(log n)
* Created two helper functions to find the first and last location of the target
* To optimize the second search i am passing the first found location as left.
* If the first helper cannot find location then there is no last location so returning [-1, -1]
* Else if we find both returning the start and end indices.
*
*/

var searchRange = function (nums, target) {
let left = 0, right = nums.length - 1;
// Method to get the first occurence of the target
const firstLocBinarySearch = (low, high) => {
let firstFound = -1;
while (low <= high) {
const mid = Math.floor(low + (high - low) / 2);
if (nums[mid] === target) {
if (firstFound === -1) firstFound = mid;
// if mid == 0, there is no mid-1 and that is the first entry of the target
if (mid === 0 || nums[mid] !== nums[mid - 1]) return [firstFound, mid];
else high = mid - 1;
} else if (nums[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return [-1, -1];
}

// Method to get the last occurence of the target
const lastLocBinarySearch = (low, high) => {
while (low <= high) {
const mid = Math.floor(low + (high - low) / 2);
if (nums[mid] === target) {
// if mid == nums.length-1, there is no mid+1 and that is the last entry of the target
if (mid === nums.length - 1 || nums[mid] !== nums[mid + 1]) return mid;
else low = mid + 1;
} else if (nums[mid] < target) {
low = mid + 1;
} else {
high = mid - 1;
}

}

return -1;
}
// get the firstfound location and the startIndex
let [firstFound, startIndex] = firstLocBinarySearch(left, right);
// if firstFound is -1 then the element is not found, can ignore the remaining code and return [-1,-1]
if (firstFound === -1) return [-1, -1];
let endIndex = lastLocBinarySearch(startIndex, right);
return [startIndex, endIndex];
};