Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the first element as the minimum if the array is not rotated.
DSA C#
if (nums[0] < nums[nums.Length - 1]) return nums[1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using nums[1] instead of nums[0]
Using nums[nums.Length] which is out of bounds
✗ Incorrect
If the first element is less than the last, the array is not rotated, so the minimum is the first element nums[0].
2fill in blank
mediumComplete the code to calculate the middle index correctly.
DSA C#
int mid = [1] + (right - left) / 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using (left + right) / 2 which can cause overflow
Using right directly instead of left
✗ Incorrect
The middle index is calculated from the left index plus half the distance to right to avoid overflow.
3fill in blank
hardFix the error in the condition to decide which half to search next.
DSA C#
if (nums[mid] [1] nums[right]) left = mid + 1; else right = mid;
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causing wrong half to be searched
Using == or != which are incorrect for this logic
✗ Incorrect
If nums[mid] is greater than nums[right], the minimum is in the right half, so move left to mid + 1.
4fill in blank
hardFill both blanks to complete the while loop condition and return statement.
DSA C#
while ([1] < [2]) { int mid = left + (right - left) / 2; if (nums[mid] > nums[right]) left = mid + 1; else right = mid; } return nums[left];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or nums.Length - 1 in the loop condition incorrectly
Returning nums[right] instead of nums[left]
✗ Incorrect
The loop runs while left is less than right, narrowing the search. The minimum is at nums[left] after the loop.
5fill in blank
hardFill all three blanks to complete the method that finds the minimum in a rotated sorted array.
DSA C#
public int FindMin(int[] nums) {
int left = [1], right = [2];
if (nums[left] < nums[right]) return nums[3];
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) left = mid + 1;
else right = mid;
}
return nums[left];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong indices for left and right initialization
Returning nums[left] before the loop instead of nums[0]
✗ Incorrect
Initialize left to 0 and right to last index. If array is not rotated, return nums[0]. Then use binary search to find minimum.
