0
0
DSA Typescriptprogramming

Find Minimum in Rotated Sorted Array in DSA Typescript

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 clock starts again after the rotation.
Original sorted array:
[1] -> [2] -> [3] -> [4] -> [5] -> null

Rotated array example:
[4] -> [5] -> [1] -> [2] -> [3] -> null
↑ (rotation point, smallest element)
Dry Run Walkthrough
Input: array: [4, 5, 1, 2, 3]
Goal: Find the smallest number in the rotated sorted array
Step 1: Set left=0, right=4 (start and end indices)
array: [4, 5, 1, 2, 3], left=0, right=4
Why: We start searching between the whole array
Step 2: Calculate mid = (0 + 4) // 2 = 2, check array[mid]=1
array: [4, 5, 1, 2, 3], left=0, mid=2, right=4
Why: Mid helps us decide which half to search next
Step 3: Compare array[mid]=1 with array[right]=3; since 1 < 3, move right to mid=2
array: [4, 5, 1, 2, 3], left=0, right=2
Why: Minimum must be at mid or to the left because mid is smaller than right
Step 4: Calculate mid = (0 + 2) // 2 = 1, check array[mid]=5
array: [4, 5, 1, 2, 3], left=0, mid=1, right=2
Why: Narrowing down the search to find the smallest element
Step 5: Compare array[mid]=5 with array[right]=1; since 5 > 1, move left to mid+1=2
array: [4, 5, 1, 2, 3], left=2, right=2
Why: Minimum must be to the right of mid because mid is greater than right
Result:
array: [4, 5, 1, 2, 3], left=2, right=2 -> minimum is array[2] = 1
Annotated Code
DSA Typescript
class RotatedSortedArray {
  static findMin(nums: number[]): number {
    let left = 0;
    let right = nums.length - 1;

    while (left < right) {
      const mid = Math.floor((left + right) / 2);
      if (nums[mid] > nums[right]) {
        left = mid + 1; // minimum is in right half
      } else {
        right = mid; // minimum is at mid or left half
      }
    }
    return nums[left];
  }
}

// Driver code
const arr = [4, 5, 1, 2, 3];
console.log(RotatedSortedArray.findMin(arr));
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 (nums[mid] > nums[right]) {
if mid element is greater than right, minimum is in right half
left = mid + 1; // minimum is in right half
move left pointer to mid+1 to discard left half
else { right = mid; // minimum is at mid or left half }
otherwise, minimum is at mid or left half, move right pointer
return nums[left];
left points to minimum element after loop ends
OutputSuccess
1
Complexity Analysis
Time: O(log n) because the search space halves each step using binary search
Space: O(1) because only a few variables are used regardless of input size
vs Alternative: Naive approach scans all elements O(n), this binary search approach is faster for large arrays
Edge Cases
Array not rotated (e.g., [1, 2, 3, 4, 5])
Algorithm returns the first element as minimum correctly
DSA Typescript
while (left < right) {
Array with one element (e.g., [10])
Returns the single element as minimum without entering loop
DSA Typescript
while (left < right) {
Array rotated at last element (e.g., [2, 3, 4, 5, 1])
Algorithm finds the minimum at the last index correctly
DSA Typescript
if (nums[mid] > nums[right]) {
When to Use This Pattern
When you see a sorted array that might be rotated and need the smallest element, use binary search on the rotation point to find it efficiently.
Common Mistakes
Mistake: Comparing nums[mid] with nums[left] instead of nums[right]
Fix: Compare nums[mid] with nums[right] to decide which half contains the minimum
Mistake: Using <= instead of < in while loop causing infinite loop
Fix: Use while (left < right) to ensure loop ends when pointers meet
Summary
Finds the smallest element in a rotated sorted array using binary search.
Use when you have a sorted array shifted and want to find the rotation point quickly.
The smallest element is where the order breaks; binary search narrows down this point efficiently.