0
0
DSA C++programming~10 mins

Find Minimum in Rotated Sorted Array in DSA C++ - 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?
Yes
Set low=0, high=n-1
While low < high
Find mid = (low+high)/2
Compare mid element with high element
Repeat loop
Return element at low as minimum
We use two pointers low and high to narrow down the minimum element by comparing mid and high elements until low meets high.
Execution Sample
DSA C++
int findMin(vector<int>& nums) {
  int low = 0, high = nums.size() - 1;
  while (low < high) {
    int mid = low + (high - low) / 2;
    if (nums[mid] > nums[high]) low = mid + 1;
    else high = mid;
  }
  return nums[low];
}
This code finds the minimum element in a rotated sorted array using binary search.
Execution Table
StepOperationlowhighmidnums[mid]nums[high]Pointer ChangesVisual State
1Initialize pointers06---low=0, high=6[4,5,6,7,0,1,2]
2Calculate mid06372-[4,5,6,7,0,1,2]
3Compare nums[mid] > nums[high]063727 > 2 is True[4,5,6,7,0,1,2]
4Update low = mid + 146---low=4[4,5,6,7,0,1,2]
5Calculate mid46512-[4,5,6,7,0,1,2]
6Compare nums[mid] > nums[high]465121 > 2 is False[4,5,6,7,0,1,2]
7Update high = mid45---high=5[4,5,6,7,0,1,2]
8Calculate mid45401-[4,5,6,7,0,1,2]
9Compare nums[mid] > nums[high]454010 > 1 is False[4,5,6,7,0,1,2]
10Update high = mid44---high=4[4,5,6,7,0,1,2]
11Loop ends as low == high44----[4,5,6,7,0,1,2]
12Return nums[low]44-0--[4,5,6,7,0,1,2]
💡 Loop ends when low equals high, pointing to the minimum element.
Variable Tracker
VariableStartAfter Step 4After Step 7After Step 10Final
low04444
high66544
mid-3544
nums[mid]-7100
nums[high]-2100
Key Moments - 3 Insights
Why do we compare nums[mid] with nums[high] instead of nums[low]?
Comparing nums[mid] with nums[high] helps us decide which half contains the minimum. If nums[mid] is greater than nums[high], the minimum is in the right half, so we move low up (see steps 3 and 4). Otherwise, it's in the left half or at mid, so we move high down (steps 6 and 7).
Why does the loop stop when low equals high?
When low equals high, it means we've narrowed down to one element, which must be the minimum (step 11). Continuing would not change the result.
What if the array is not rotated?
If the array is not rotated, nums[mid] will always be less than or equal to nums[high], so high moves down until low equals 0, returning the first element as minimum.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of low after step 4?
A3
B0
C4
D6
💡 Hint
Check the 'Pointer Changes' and 'low' column in row for step 4.
At which step does the loop end because low equals high?
AStep 11
BStep 12
CStep 10
DStep 9
💡 Hint
Look for the row where 'Loop ends as low == high' in the 'Operation' column.
If nums[mid] was always less than nums[high], which pointer would move?
Alow pointer moves up
Bhigh pointer moves down
Cboth pointers move
Dno pointer moves
💡 Hint
Refer to steps 6 and 9 where nums[mid] <= nums[high] and high is updated.
Concept Snapshot
Find Minimum in Rotated Sorted Array:
- Use binary search with low=0, high=n-1
- While low < high:
  - mid = (low+high)/2
  - If nums[mid] > nums[high], low = mid+1
  - Else high = mid
- Return nums[low] as minimum
- Works because rotation splits sorted array into two sorted parts
Full Transcript
This concept shows how to find the minimum element in a rotated sorted array using binary search. We start with two pointers, low at 0 and high at the last index. We calculate mid and compare nums[mid] with nums[high]. If nums[mid] is greater, the minimum lies to the right, so we move low up. Otherwise, the minimum is at mid or to the left, so we move high down. We repeat until low equals high, which points to the minimum element. This method efficiently narrows down the search space by half each time.