0
0
DSA Javascriptprogramming~10 mins

Find Minimum in Rotated Sorted Array in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Find Minimum in Rotated Sorted Array
Start with full array
Check if array is rotated?
Use binary search
Find mid element
Compare mid with right
Search right half
Repeat until left == right
Return element at left
Start by checking if array is rotated. If yes, use binary search comparing mid and right elements to narrow down the minimum until left meets right.
Execution Sample
DSA Javascript
function findMin(nums) {
  let left = 0, right = nums.length - 1;
  while (left < right) {
    let mid = Math.floor((left + right) / 2);
    if (nums[mid] > nums[right]) left = mid + 1;
    else right = mid;
  }
  return nums[left];
}
This code finds the minimum element in a rotated sorted array using binary search.
Execution Table
StepOperationLeft IndexRight IndexMid IndexCompare nums[mid] and nums[right]Pointer ChangesArray StateCurrent Minimum Candidate
1Initialize pointers06--left=0, right=6[4,5,6,7,0,1,2]N/A
2Calculate mid063nums[3]=7 > nums[6]=2left = mid + 1 = 4[4,5,6,7,0,1,2]N/A
3Calculate mid465nums[5]=1 <= nums[6]=2right = mid = 5[4,5,6,7,0,1,2]N/A
4Calculate mid454nums[4]=0 <= nums[5]=1right = mid = 4[4,5,6,7,0,1,2]N/A
5Check loop condition44--left == right, loop ends[4,5,6,7,0,1,2]nums[4] = 0
💡 left equals right at index 4, minimum element found at nums[4] = 0
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
left04444
right66544
mid-3544
Key Moments - 3 Insights
Why do we compare nums[mid] with nums[right] instead of nums[left]?
Comparing nums[mid] with nums[right] helps decide which half contains the minimum because the rightmost element is a reliable pivot in rotated arrays. See execution_table rows 2-4 where this comparison guides pointer moves.
Why does the loop stop when left equals right?
When left equals right, the search space is narrowed down to one element, which must be the minimum. This is shown in execution_table row 5 where left and right both are 4.
What if the array is not rotated?
If the array is not rotated, nums[mid] will always be less than or equal to nums[right], so right pointer moves left until left equals right, returning the first element. This is implied in the concept flow's 'No' branch.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'left' after step 2?
A3
B4
C0
D6
💡 Hint
Check the 'Pointer Changes' column in row 2 of execution_table.
At which step does the loop condition become false?
AStep 5
BStep 4
CStep 3
DStep 2
💡 Hint
Look at the 'Check loop condition' operation in execution_table row 5.
If nums[mid] was always less than nums[right], how would the pointers change?
Aboth pointers would stay the same
Bleft would move right
Cright would move left
Dleft would move left
💡 Hint
Refer to pointer changes in execution_table rows 3 and 4 where nums[mid] <= nums[right].
Concept Snapshot
Find Minimum in Rotated Sorted Array
- Use binary search with left and right pointers
- Compare nums[mid] with nums[right]
- If nums[mid] > nums[right], move left to mid+1
- Else move right to mid
- Loop until left == right
- Return nums[left] as minimum
Full Transcript
This concept finds the minimum element in a rotated sorted array using binary search. We start with two pointers, left at 0 and right at the last index. We check if the array is rotated by comparing elements. Using binary search, we calculate mid and compare nums[mid] with nums[right]. If nums[mid] is greater, the minimum is in the right half, so we move left to mid+1. Otherwise, the minimum is in the left half, so we move right to mid. We repeat until left equals right, which points to the minimum element. This method efficiently narrows down the search space in logarithmic time.