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 C# code that uses binary search on answer to find the minimum largest sum among subarrays?
DSA C#
using System; class Program { static bool CanSplit(int[] nums, int m, int maxSum) { int currentSum = 0, count = 1; foreach (var num in nums) { if (currentSum + num > maxSum) { count++; currentSum = num; if (count > m) return false; } else { currentSum += num; } } return true; } static int SplitArray(int[] nums, int m) { int left = 0, right = 0; foreach (var num in 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; } static void Main() { int[] nums = {7, 2, 5, 10, 8}; int m = 2; Console.WriteLine(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 [7,2,5,10,8] split into 2 subarrays to minimize the largest sum results in [7,2,5] and [10,8] with sums 14 and 18. The minimum largest sum is 18.
🧠 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 narrows down the answer range.
✗ Incorrect
The 'check' function tests if a guessed answer is valid under problem constraints, guiding the binary search to move left or right.
🔧 Debug
advancedIdentify the Output or Error of Binary Search on Answer Code
What will be the output or error of this C# code snippet using binary search on answer to find minimum time to complete tasks?
DSA C#
using System; class Program { static bool CanFinish(int[] tasks, int h, int speed) { int time = 0; foreach (var task in tasks) { time += (task + speed - 1) / speed; } return time <= h; } static int MinSpeed(int[] tasks, int h) { int left = 1, right = 1000000000; while (left < right) { int mid = left + (right - left) / 2; if (CanFinish(tasks, h, mid)) right = mid; else left = mid + 1; } return left; } static void Main() { int[] tasks = {30, 11, 23, 4, 20}; int h = 5; Console.WriteLine(MinSpeed(tasks, h)); } }
Attempts:
2 left
💡 Hint
Check how the time is calculated and the binary search range.
✗ Incorrect
The minimum speed to finish all tasks in 5 hours is 30, as speed less than 30 requires more than 5 hours.
❓ Predict Output
advancedOutput of Binary Search on Answer for Aggressive Cows Problem
What is the output of this C# code that uses binary search on answer to place cows in stalls maximizing minimum distance?
DSA C#
using System; class Program { static bool CanPlace(int[] stalls, int cows, int dist) { int count = 1, lastPos = stalls[0]; for (int i = 1; i < stalls.Length; i++) { if (stalls[i] - lastPos >= dist) { count++; lastPos = stalls[i]; if (count == cows) return true; } } return false; } static int MaxMinDistance(int[] stalls, int cows) { Array.Sort(stalls); int left = 0, right = stalls[stalls.Length - 1] - stalls[0]; int result = 0; while (left <= right) { int mid = left + (right - left) / 2; if (CanPlace(stalls, cows, mid)) { result = mid; left = mid + 1; } else { right = mid - 1; } } return result; } static void Main() { int[] stalls = {1, 2, 8, 4, 9}; int cows = 3; Console.WriteLine(MaxMinDistance(stalls, cows)); } }
Attempts:
2 left
💡 Hint
Try placing cows with minimum distance 3 and check feasibility.
✗ Incorrect
The largest minimum distance to place 3 cows in stalls is 3, placing them at positions 1, 4, and 8 (distances 3 and 4) or 1, 4, 9.
🧠 Conceptual
expertWhy is Binary Search on Answer Efficient for Optimization Problems?
Why does the binary search on answer technique provide an efficient solution for certain optimization problems?
Attempts:
2 left
💡 Hint
Think about how binary search narrows down the range of possible answers.
✗ Incorrect
Binary search on answer narrows the range of possible answers by checking if a guess is valid, reducing complexity from linear to logarithmic in the answer space.
