Challenge - 5 Problems
Binary Search on Answer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Binary Search on Answer for Minimum Maximum Subarray Sum
What is the output of the following code that uses binary search on answer to find the minimum largest sum among subarrays?
DSA C
int canSplit(int* nums, int numsSize, int maxSum, int m) { int currentSum = 0, count = 1; for (int i = 0; i < numsSize; i++) { if (nums[i] > maxSum) return 0; if (currentSum + nums[i] > maxSum) { count++; currentSum = nums[i]; if (count > m) return 0; } else { currentSum += nums[i]; } } return 1; } int splitArray(int* nums, int numsSize, int m) { int left = 0, right = 0; for (int i = 0; i < numsSize; i++) right += nums[i]; int result = right; while (left <= right) { int mid = left + (right - left) / 2; if (canSplit(nums, numsSize, mid, m)) { result = mid; right = mid - 1; } else { left = mid + 1; } } return result; } #include <stdio.h> int main() { int nums[] = {7, 2, 5, 10, 8}; int m = 2; int result = splitArray(nums, 5, m); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Think about how the binary search narrows down the maximum allowed subarray sum.
✗ Incorrect
The code finds the minimum largest sum when splitting the array into 2 subarrays. The answer is 18 because splitting as [7,2,5] and [10,8] gives sums 14 and 18, with 18 being the largest minimal sum possible.
🧠 Conceptual
intermediateUnderstanding the Role of the Check Function in Binary Search on Answer
In the binary search on answer technique, what is the main purpose of the 'check' function?
Attempts:
2 left
💡 Hint
Think about how binary search decides to move left or right.
✗ Incorrect
The check function tests if the current guessed answer is valid under problem rules, guiding the binary search to narrow the search space.
🔧 Debug
advancedIdentify the Error in Binary Search on Answer Implementation
What error will the following code produce when running binary search on answer to find the minimum maximum time to complete tasks?
DSA C
int canFinish(int* tasks, int tasksSize, int maxTime, int workers) { int count = 1, currentTime = 0; for (int i = 0; i < tasksSize; i++) { if (tasks[i] > maxTime) return 0; if (currentTime + tasks[i] > maxTime) { count++; currentTime = tasks[i]; if (count > workers) return 0; } else { currentTime += tasks[i]; } } return 1; } int minTime(int* tasks, int tasksSize, int workers) { int left = 0, right = 0; for (int i = 0; i < tasksSize; i++) right += tasks[i]; int result = right; while (left < right) { int mid = (left + right) / 2; if (canFinish(tasks, tasksSize, mid, workers)) { result = mid; right = mid - 1; } else { left = mid + 1; } } return result; } #include <stdio.h> int main() { int tasks[] = {3, 6, 7, 11}; int workers = 2; int answer = minTime(tasks, 4, workers); printf("%d\n", answer); return 0; }
Attempts:
2 left
💡 Hint
Check how the binary search updates left and right pointers.
✗ Incorrect
The binary search uses 'right = mid;' instead of 'right = mid - 1;', causing the loop to never terminate when left == mid.
🚀 Application
advancedApplying Binary Search on Answer to Allocate Books Problem
Given an array of book pages and number of students, binary search on answer is used to find the minimum maximum pages assigned to a student. Which of the following outputs is correct for input pages = [12, 34, 67, 90], students = 2?
Attempts:
2 left
💡 Hint
Try splitting the books into 2 parts minimizing the maximum sum.
✗ Incorrect
Splitting as [12,34,67] and [90] gives max pages 90, which is minimal possible.
🧠 Conceptual
expertWhy is Binary Search on Answer Efficient for Optimization Problems?
Why does binary search on answer technique efficiently solve optimization problems where the answer lies within a numeric range?
Attempts:
2 left
💡 Hint
Think about how binary search halves the search space each step.
✗ Incorrect
Binary search on answer narrows down the possible answer range by checking if a candidate answer is feasible, cutting the search space in half each time.
