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 Rust code that uses binary search on answer to find the minimum largest sum among subarrays?
DSA Rust
fn can_split(nums: &[i32], max_sum: i32, m: i32) -> bool { let mut current_sum = 0; let mut count = 1; for &num in nums { if current_sum + num > max_sum { count += 1; current_sum = num; if count > m { return false; } } else { current_sum += num; } } true } fn split_array(nums: Vec<i32>, m: i32) -> i32 { let mut left = *nums.iter().max().unwrap(); let mut right: i32 = nums.iter().sum(); while left < right { let mid = left + (right - left) / 2; if can_split(&nums, mid, m) { right = mid; } else { left = mid + 1; } } left } fn main() { let nums = vec![7, 2, 5, 10, 8]; let m = 2; println!("{}", split_array(nums, m)); }
Attempts:
2 left
💡 Hint
Think about splitting the array into 2 subarrays such that the largest sum is minimized.
✗ Incorrect
The code finds the smallest maximum subarray sum when splitting the array into 2 parts. The correct answer is 18 because splitting as [7,2,5] and [10,8] gives sums 14 and 18, and 18 is the minimal largest 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 narrows down the answer range.
✗ Incorrect
The 'check' function tests if the current guessed answer is valid under problem constraints, guiding the binary search to move left or right.
🔧 Debug
advancedIdentify the Logical Error in Binary Search on Answer Implementation
What is the logical error in the following Rust code snippet for binary search on answer?
DSA Rust
fn can_place_cows(stalls: &[i32], dist: i32, cows: i32) -> bool { let mut count = 1; let mut last_position = stalls[0]; for &stall in stalls.iter().skip(1) { if stall - last_position >= dist { count += 1; last_position = stall; } ; } count >= cows } fn aggressive_cows(stalls: Vec<i32>, cows: i32) -> i32 { let mut stalls = stalls; stalls.sort(); let mut left = 0; let mut right = stalls[stalls.len() - 1] - stalls[0]; while left < right { let mid = left + (right - left) / 2; if can_place_cows(&stalls, mid, cows) { left = mid + 1; } else { right = mid; } } left }
Attempts:
2 left
💡 Hint
Check how left and right pointers are updated in binary search.
✗ Incorrect
The binary search moves left to mid + 1 when can_place_cows returns true, which skips the correct answer. It should move right to mid to find the maximum minimum distance.
❓ Predict Output
advancedOutput of Binary Search on Answer for Painter Partition Problem
What is the output of this Rust code that uses binary search on answer to find the minimum time to paint all boards by k painters?
DSA Rust
fn is_possible(boards: &[i32], k: i32, mid: i32) -> bool { let mut painters = 1; let mut time = 0; for &board in boards { if board > mid { return false; } if time + board > mid { painters += 1; time = board; if painters > k { return false; } } else { time += board; } } true } fn min_time(boards: Vec<i32>, k: i32) -> i32 { let mut left = *boards.iter().max().unwrap(); let mut right: i32 = boards.iter().sum(); while left < right { let mid = left + (right - left) / 2; if is_possible(&boards, k, mid) { right = mid; } else { left = mid + 1; } } left } fn main() { let boards = vec![10, 20, 30, 40]; let k = 2; println!("{}", min_time(boards, k)); }
Attempts:
2 left
💡 Hint
Try splitting boards between 2 painters to minimize the maximum painting time.
✗ Incorrect
Splitting as [10,20,30] and [40] gives max time 60, but [10,20] and [30,40] gives max time 70. The minimal max time is 50 by splitting as [10,20] and [30,40].
🧠 Conceptual
expertWhy is Binary Search on Answer Suitable for Optimization Problems?
Why is the binary search on answer technique particularly suitable for optimization problems with monotonic constraints?
Attempts:
2 left
💡 Hint
Think about how binary search exploits order in the solution space.
✗ Incorrect
Binary search on answer works when the feasibility of a guessed answer changes monotonically, enabling efficient search without checking all possibilities.
