0
0
DSA Javascriptprogramming

Find Minimum in Rotated Sorted Array in DSA Javascript

Choose your learning style9 modes available
Mental Model
A rotated sorted array is like a sorted list that got shifted. The smallest number is where the shift happened.
Analogy: Imagine a clock face with numbers 1 to 12 in order. If you rotate the clock, the smallest number is where the new 'start' of the clock is.
Original sorted array:
[1] -> [2] -> [3] -> [4] -> [5] -> null

Rotated array example:
[4] -> [5] -> [1] -> [2] -> [3] -> null
↑ (rotation point, minimum)
Dry Run Walkthrough
Input: array: [4, 5, 1, 2, 3]
Goal: Find the smallest number in the rotated sorted array efficiently
Step 1: Set left=0, right=4 (start and end indices)
Array: [4, 5, 1, 2, 3]
left=0 -> 4, right=4 -> 3
Why: We start searching between the whole array
Step 2: Calculate mid = Math.floor((0+4)/2) = 2, check value at mid=1
Array: [4, 5, 1, 2, 3]
left=0 -> 4, mid=2 -> 1, right=4 -> 3
Why: Mid helps us decide which half contains the minimum
Step 3: Compare mid value 1 with right value 3; since 1 < 3, move right to mid
Array: [4, 5, 1, 2, 3]
left=0 -> 4, right=2 -> 1
Why: Minimum must be at mid or to the left
Step 4: Calculate new mid = Math.floor((0+2)/2) = 1, mid value=5
Array: [4, 5, 1, 2, 3]
left=0 -> 4, mid=1 -> 5, right=2 -> 1
Why: Check middle of new search range
Step 5: Compare mid value 5 with right value 1; since 5 > 1, move left to mid+1
Array: [4, 5, 1, 2, 3]
left=2 -> 1, right=2 -> 1
Why: Minimum is to the right of mid
Step 6: Now left == right == 2, found minimum at index 2 with value 1
Array: [4, 5, 1, 2, 3]
Minimum = 1 at index 2
Why: Search narrowed to one element which is minimum
Result:
Minimum value is 1 at index 2
Annotated Code
DSA Javascript
class RotatedSortedArray {
  constructor(nums) {
    this.nums = nums;
  }

  findMin() {
    let left = 0;
    let right = this.nums.length - 1;

    while (left < right) {
      const mid = Math.floor((left + right) / 2);
      // If mid element is less than right element, min is at mid or left side
      if (this.nums[mid] < this.nums[right]) {
        right = mid; // move right to mid
      } else {
        left = mid + 1; // min is to the right of mid
      }
    }
    return this.nums[left]; // left == right is min index
  }
}

// Driver code
const arr = new RotatedSortedArray([4, 5, 1, 2, 3]);
console.log(arr.findMin());
while (left < right) {
loop until search space narrows to one element
const mid = Math.floor((left + right) / 2);
find middle index to split search space
if (this.nums[mid] < this.nums[right]) {
if mid value less than right, min is at mid or left side
right = mid;
move right pointer to mid to narrow search left
else { left = mid + 1; }
else min is right of mid, move left pointer
return this.nums[left];
return minimum value when left meets right
OutputSuccess
1
Complexity Analysis
Time: O(log n) because we halve the search space each step using binary search
Space: O(1) because we use only a few variables, no extra space proportional to input
vs Alternative: Naive approach scans all elements O(n), this binary search method is faster for large arrays
Edge Cases
Array not rotated (e.g. [1, 2, 3, 4, 5])
Algorithm returns first element as minimum correctly
DSA Javascript
if (this.nums[mid] < this.nums[right]) { right = mid; }
Array with single element (e.g. [10])
Returns the single element as minimum
DSA Javascript
while (left < right) { ... } // loop skipped as left == right
Array rotated at last element (e.g. [2, 3, 4, 5, 1])
Correctly finds minimum at last index
DSA Javascript
else { left = mid + 1; }
When to Use This Pattern
When you see a sorted array that might be rotated and need the smallest element quickly, use binary search to find the rotation point efficiently.
Common Mistakes
Mistake: Comparing mid element with left instead of right pointer
Fix: Always compare mid with right element to decide search direction
Mistake: Using <= in while loop causing infinite loop
Fix: Use while (left < right) to ensure loop ends when pointers meet
Summary
Finds the smallest number in a rotated sorted array using binary search.
Use when you have a sorted array shifted and want to find the minimum quickly.
The minimum is the only element smaller than the element at the right pointer in the search range.