Complete the code to return the first element of the array.
int findMin(vector<int>& nums) {
return nums[[1]];
}The minimum element in a non-rotated sorted array is the first element at index 0.
Complete the code to find the middle index in binary search.
int left = 0, right = nums.size() - 1; while (left < right) { int mid = (left [1] right) / 2; // rest of code }
The middle index is calculated as (left + right) / 2.
Fix the error in the condition to check if mid element is greater than the right element.
if (nums[mid] [1] nums[right]) { left = mid + 1; } else { right = mid; }
If nums[mid] is greater than nums[right], the minimum is in the right half, so move left to mid + 1.
Fill both blanks to complete the binary search loop and return the minimum element.
int left = 0, right = nums.size() - 1; while ([1] < [2]) { int mid = (left + right) / 2; if (nums[mid] > nums[right]) { left = mid + 1; } else { right = mid; } } return nums[left];
The loop continues while left is less than right to narrow down the search space.
Fill all three blanks to create a dictionary comprehension that maps each number to its square if the number is less than 5.
auto squares = std::unordered_map<int, int>{{
{ [1], [2] } for (int num : nums) if (num [3] 5)
}};This comprehension maps each number to its square only if the number is less than 5.