Challenge - 5 Problems
Maximum Subarray Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Maximum Subarray Sum Using Divide and Conquer
What is the output of this Rust code that finds the maximum subarray sum using divide and conquer?
DSA Rust
fn max_crossing_sum(arr: &[i32], left: usize, mid: usize, right: usize) -> i32 {
let mut sum = 0;
let mut left_sum = i32::MIN;
for i in (left..=mid).rev() {
sum += arr[i];
if sum > left_sum {
left_sum = sum;
}
}
sum = 0;
let mut right_sum = i32::MIN;
for i in mid + 1..=right {
sum += arr[i];
if sum > right_sum {
right_sum = sum;
}
}
left_sum + right_sum
}
fn max_subarray_sum(arr: &[i32], left: usize, right: usize) -> i32 {
if left == right {
return arr[left];
}
let mid = (left + right) / 2;
let left_max = max_subarray_sum(arr, left, mid);
let right_max = max_subarray_sum(arr, mid + 1, right);
let cross_max = max_crossing_sum(arr, left, mid, right);
*[left_max, right_max, cross_max].iter().max().unwrap()
}
fn main() {
let arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
let max_sum = max_subarray_sum(&arr, 0, arr.len() - 1);
println!("{}", max_sum);
}Attempts:
2 left
💡 Hint
Think about the subarray with the largest sum that crosses the middle point.
✗ Incorrect
The maximum subarray is [4, -1, 2, 1] which sums to 6. The divide and conquer approach finds this by checking left, right, and crossing sums.
🧠 Conceptual
intermediateUnderstanding the Divide and Conquer Approach for Maximum Subarray
Which statement best describes the divide and conquer approach to find the maximum subarray sum?
Attempts:
2 left
💡 Hint
Divide and conquer splits the problem into smaller parts and combines results.
✗ Incorrect
The divide and conquer method splits the array, solves for left and right halves, and also considers subarrays crossing the midpoint.
🔧 Debug
advancedIdentify the Error in Maximum Subarray Divide and Conquer Code
What error will this Rust code produce when trying to find the maximum subarray sum?
DSA Rust
fn max_crossing_sum(arr: &[i32], left: usize, mid: usize, right: usize) -> i32 {
let mut sum = 0;
let mut left_sum = i32::MIN;
for i in left..=mid {
sum += arr[i];
if sum > left_sum {
left_sum = sum;
}
}
sum = 0;
let mut right_sum = i32::MIN;
for i in mid+1..=right {
sum += arr[i];
if sum > right_sum {
right_sum = sum;
}
}
left_sum + right_sum
}
fn max_subarray_sum(arr: &[i32], left: usize, right: usize) -> i32 {
if left == right {
return arr[left];
}
let mid = (left + right) / 2;
let left_max = max_subarray_sum(arr, left, mid);
let right_max = max_subarray_sum(arr, mid + 1, right);
let cross_max = max_crossing_sum(arr, left, mid, right);
*[left_max, right_max, cross_max].iter().max().unwrap()
}
fn main() {
let arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
let max_sum = max_subarray_sum(&arr, 0, arr.len() - 1);
println!("{}", max_sum);
}Attempts:
2 left
💡 Hint
Check the parameters passed to the recursive function calls.
✗ Incorrect
The call max_subarray_sum(&arr, 0, arr.len()) uses arr.len() as right index, which is out of bounds since valid indices are 0 to arr.len()-1.
❓ Predict Output
advancedOutput of Maximum Subarray Sum for All Negative Numbers
What is the output of this Rust code when the input array contains all negative numbers?
DSA Rust
fn max_crossing_sum(arr: &[i32], left: usize, mid: usize, right: usize) -> i32 {
let mut sum = 0;
let mut left_sum = i32::MIN;
for i in (left..=mid).rev() {
sum += arr[i];
if sum > left_sum {
left_sum = sum;
}
}
sum = 0;
let mut right_sum = i32::MIN;
for i in mid + 1..=right {
sum += arr[i];
if sum > right_sum {
right_sum = sum;
}
}
left_sum + right_sum
}
fn max_subarray_sum(arr: &[i32], left: usize, right: usize) -> i32 {
if left == right {
return arr[left];
}
let mid = (left + right) / 2;
let left_max = max_subarray_sum(arr, left, mid);
let right_max = max_subarray_sum(arr, mid + 1, right);
let cross_max = max_crossing_sum(arr, left, mid, right);
*[left_max, right_max, cross_max].iter().max().unwrap()
}
fn main() {
let arr = [-8, -3, -6, -2, -5, -4];
let max_sum = max_subarray_sum(&arr, 0, arr.len() - 1);
println!("{}", max_sum);
}Attempts:
2 left
💡 Hint
The maximum subarray in all negative numbers is the least negative single element.
✗ Incorrect
The maximum subarray sum is -2, which is the largest (least negative) element in the array.
🚀 Application
expertMaximum Subarray Sum with Indices Using Divide and Conquer
Which option correctly modifies the divide and conquer maximum subarray code to also return the start and end indices of the maximum subarray?
Attempts:
2 left
💡 Hint
Think about how to carry index information through recursion.
✗ Incorrect
Returning a tuple with sum and indices allows tracking the exact subarray during recursion and combining results correctly.
