0
0
DSA C++programming~20 mins

Find Minimum in Rotated Sorted Array in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rotated Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find minimum element output after rotation
What is the output of the following C++ code that finds the minimum element in a rotated sorted array?
DSA C++
int findMin(vector<int>& nums) {
    int left = 0, right = nums.size() - 1;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] > nums[right]) {
            left = mid + 1;
        } else {
            right = mid;
        }
    }
    return nums[left];
}

int main() {
    vector<int> nums = {4,5,6,7,0,1,2};
    cout << findMin(nums) << endl;
    return 0;
}
A2
B1
C0
D4
Attempts:
2 left
💡 Hint
Think about where the smallest element lies in a rotated sorted array.
Predict Output
intermediate
2:00remaining
Minimum element in a rotated array with duplicates
What will be the output of this code when the input array contains duplicates?
DSA C++
int findMin(vector<int>& nums) {
    int left = 0, right = nums.size() - 1;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] > nums[right]) {
            left = mid + 1;
        } else if (nums[mid] < nums[right]) {
            right = mid;
        } else {
            right--;
        }
    }
    return nums[left];
}

int main() {
    vector<int> nums = {2,2,2,0,1,2};
    cout << findMin(nums) << endl;
    return 0;
}
A2
B0
C1
DError
Attempts:
2 left
💡 Hint
Duplicates require a slight modification in the binary search approach.
🔧 Debug
advanced
2:00remaining
Identify the error in this minimum finder code
What error will this code produce when trying to find the minimum in a rotated sorted array?
DSA C++
int findMin(vector<int>& nums) {
    int left = 0, right = nums.size() - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] > nums[right]) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return nums[left];
}

int main() {
    vector<int> nums = {3,4,5,1,2};
    cout << findMin(nums) << endl;
    return 0;
}
ARuntime error: out_of_range
BOutput: 1
COutput: 3
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop condition and how left and right pointers move.
Predict Output
advanced
2:00remaining
Output of minimum finder on a non-rotated sorted array
What is the output of this code when the input array is sorted but not rotated?
DSA C++
int findMin(vector<int>& nums) {
    int left = 0, right = nums.size() - 1;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] > nums[right]) {
            left = mid + 1;
        } else {
            right = mid;
        }
    }
    return nums[left];
}

int main() {
    vector<int> nums = {1,2,3,4,5};
    cout << findMin(nums) << endl;
    return 0;
}
A1
B5
C3
D0
Attempts:
2 left
💡 Hint
If the array is not rotated, the minimum is the first element.
🧠 Conceptual
expert
2:00remaining
Why binary search works for finding minimum in rotated sorted array
Which statement best explains why binary search can be used to find the minimum element in a rotated sorted array?
ABecause the array elements are unique and binary search only works on unique elements.
BBecause the array is rotated, the minimum element is always the last element.
CBecause the minimum element is always at the middle index after rotation.
DBecause the array is sorted, and rotation preserves sorted order in two parts, allowing binary search to discard half the array each step.
Attempts:
2 left
💡 Hint
Think about how rotation affects sorted order and how binary search uses order.