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 Java code that uses binary search on answer to find the minimum largest sum among subarrays?
DSA Java
public class Solution { public static boolean canSplit(int[] nums, int m, int maxSum) { int currentSum = 0, count = 1; for (int num : nums) { if (currentSum + num > maxSum) { count++; currentSum = num; if (count > m) return false; } else { currentSum += num; } } return true; } public static int splitArray(int[] nums, int m) { int left = 0, right = 0; for (int num : nums) { left = Math.max(left, num); right += num; } while (left < right) { int mid = left + (right - left) / 2; if (canSplit(nums, m, mid)) { right = mid; } else { left = mid + 1; } } return left; } public static void main(String[] args) { int[] nums = {7, 2, 5, 10, 8}; int m = 2; System.out.println(splitArray(nums, m)); } }
Attempts:
2 left
💡 Hint
Think about how the array can be split into 2 subarrays with minimum largest sum.
✗ Incorrect
The array can be split as [7,2,5] and [10,8] with sums 14 and 18. The minimum largest sum possible is 18.
🧠 Conceptual
intermediateUnderstanding the Role of the 'canSplit' Function
In the binary search on answer technique, what is the main purpose of the 'canSplit' function in the context of splitting an array into subarrays?
Attempts:
2 left
💡 Hint
Think about the condition that guides the binary search direction.
✗ Incorrect
The 'canSplit' function verifies if a candidate maxSum allows splitting the array into m or fewer subarrays without exceeding maxSum in any subarray.
❓ Predict Output
advancedOutput of Binary Search on Answer for Minimum Time to Complete Jobs
What is the output of the following Java code that uses binary search on answer to find the minimum time to complete all jobs by workers?
DSA Java
public class Solution { public static boolean canFinish(int[] jobs, int workers, int maxTime) { int count = 1, currentTime = 0; for (int job : jobs) { if (job > maxTime) return false; if (currentTime + job > maxTime) { count++; currentTime = job; if (count > workers) return false; } else { currentTime += job; } } return true; } public static int minTime(int[] jobs, int workers) { int left = 0, right = 0; for (int job : jobs) right += job; while (left < right) { int mid = left + (right - left) / 2; if (canFinish(jobs, workers, mid)) { right = mid; } else { left = mid + 1; } } return left; } public static void main(String[] args) { int[] jobs = {3, 2, 3}; int workers = 3; System.out.println(minTime(jobs, workers)); } }
Attempts:
2 left
💡 Hint
Each worker can do one job, so minimum time is the longest single job time.
✗ Incorrect
With 3 workers and jobs [3,2,3], each worker can do one job. The minimum time is max job time 3.
🔧 Debug
advancedIdentify the Error in Binary Search on Answer Implementation
What error will the following Java code produce when run, and why?
DSA Java
public class Solution { public static boolean canSplit(int[] nums, int m, int maxSum) { int currentSum = 0, count = 1; for (int num : nums) { if (currentSum + num >= maxSum) { count++; currentSum = num; if (count > m) return false; } else { currentSum += num; } } return true; } public static int splitArray(int[] nums, int m) { int left = 0, right = 0; for (int num : nums) { left = Math.max(left, num); right += num; } while (left < right) { int mid = left + (right - left) / 2; if (canSplit(nums, m, mid)) { right = mid; } else { left = mid + 1; } } return left; } public static void main(String[] args) { int[] nums = {7, 2, 5, 10, 8}; int m = 2; System.out.println(splitArray(nums, m)); } }
Attempts:
2 left
💡 Hint
Check the condition that triggers a new subarray split.
✗ Incorrect
Using '>=' causes splitting even when sum equals maxSum, leading to more splits and a smaller max sum than correct.
🚀 Application
expertMinimum Capacity to Ship Packages Within D Days
Given weights of packages and days D, binary search on answer is used to find the minimum ship capacity to ship all packages within D days. If weights = [1,2,3,4,5,6,7,8,9,10] and D = 5, what is the minimum capacity?
Attempts:
2 left
💡 Hint
Try to split the packages into 5 days with minimum max daily weight.
✗ Incorrect
The minimum capacity is 15, allowing splits like [1,2,3,4,5], [6,7], [8], [9], [10].
