Challenge - 5 Problems
Rotated Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the minimum element output in a rotated sorted array
What is the output of the following Java code that finds the minimum element in a rotated sorted array?
DSA Java
public class Solution { public static int findMin(int[] nums) { int left = 0, right = nums.length - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[right]) { left = mid + 1; } else { right = mid; } } return nums[left]; } public static void main(String[] args) { int[] nums = {4,5,6,7,0,1,2}; System.out.println(findMin(nums)); } }
Attempts:
2 left
💡 Hint
Focus on how the binary search narrows down the minimum element by comparing mid and right elements.
✗ Incorrect
The code uses binary search to find the smallest element in the rotated sorted array. Since the array is rotated, the minimum element is the only element where the previous element is greater. The algorithm compares nums[mid] with nums[right] to decide which half to search next. The output is 0, the smallest element.
❓ Predict Output
intermediateOutput of minimum finder on a non-rotated sorted array
What will be the output of the minimum finder code when the input array is not rotated but sorted?
DSA Java
public class Solution { public static int findMin(int[] nums) { int left = 0, right = nums.length - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[right]) { left = mid + 1; } else { right = mid; } } return nums[left]; } public static void main(String[] args) { int[] nums = {1,2,3,4,5,6,7}; System.out.println(findMin(nums)); } }
Attempts:
2 left
💡 Hint
Consider that the array is already sorted and not rotated.
✗ Incorrect
When the array is sorted and not rotated, the minimum element is the first element. The binary search will keep narrowing down to the leftmost element, which is 1.
🔧 Debug
advancedIdentify 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 Java
public class Solution { public static int findMin(int[] nums) { int left = 0, right = nums.length - 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]; } public static void main(String[] args) { int[] nums = {4,5,0,1,2}; System.out.println(findMin(nums)); } }
Attempts:
2 left
💡 Hint
Check the loop condition and how left and right pointers are updated.
✗ Incorrect
The loop condition uses <= (should be <) and the right pointer is updated to mid - 1 (should be mid), which causes the code to skip the mid element that could be the minimum, returning wrong value. For {4,5,0,1,2}, it returns 4 instead of 0.
🧠 Conceptual
advancedWhy binary search works in rotated sorted arrays for finding minimum
Why does binary search work to find the minimum element in a rotated sorted array?
Attempts:
2 left
💡 Hint
Think about how rotation affects sorting and how comparisons guide the search.
✗ Incorrect
The rotated sorted array is partially sorted. By comparing the middle element with the rightmost element, we can determine which half contains the minimum element and discard the other half, making binary search effective.
❓ Predict Output
expertOutput of minimum finder with duplicates in rotated sorted array
What is the output of the following code when the rotated sorted array contains duplicates?
DSA Java
public class Solution { public static int findMin(int[] nums) { int left = 0, right = nums.length - 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]; } public static void main(String[] args) { int[] nums = {2,2,2,0,1,2}; System.out.println(findMin(nums)); } }
Attempts:
2 left
💡 Hint
Duplicates require an extra step to reduce the search space safely.
✗ Incorrect
When duplicates exist, if nums[mid] equals nums[right], we reduce right by one to shrink the search space without missing the minimum. The minimum element is 0 in the given array.
