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 the following code that finds the maximum subarray sum using divide and conquer?
DSA Python
def max_crossing_sum(arr, left, mid, right): left_sum = float('-inf') total = 0 for i in range(mid, left - 1, -1): total += arr[i] if total > left_sum: left_sum = total right_sum = float('-inf') total = 0 for i in range(mid + 1, right + 1): total += arr[i] if total > right_sum: right_sum = total return left_sum + right_sum def max_subarray_sum(arr, left, right): if left == right: return arr[left] mid = (left + right) // 2 left_max = max_subarray_sum(arr, left, mid) right_max = max_subarray_sum(arr, mid + 1, right) cross_max = max_crossing_sum(arr, left, mid, right) return max(left_max, right_max, cross_max) arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4] print(max_subarray_sum(arr, 0, len(arr) - 1))
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 of the following 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 recursively, and also considers subarrays crossing the middle to find the maximum sum.
🔧 Debug
advancedIdentify the Error in Maximum Subarray Divide and Conquer Code
What error does the following code produce when run?
DSA Python
def max_crossing_sum(arr, left, mid, right): left_sum = float('-inf') total = 0 for i in range(mid, left + 1): total += arr[i] if total > left_sum: left_sum = total right_sum = float('-inf') total = 0 for i in range(mid + 1, right + 1): total += arr[i] if total > right_sum: right_sum = total return left_sum + right_sum arr = [1, -2, 3, 4, -1, 2] print(max_crossing_sum(arr, 0, 2, 5))
Attempts:
2 left
💡 Hint
Check the range parameters in the for loop iterating backwards.
✗ Incorrect
The for loop for left side uses range(mid, left + 1) which for mid=2, left=0 is range(2,1), an empty range. Thus left_sum remains float('-inf'), returning incorrect sum but no runtime error.
🚀 Application
advancedMaximum Subarray Sum for All Negative Numbers
What is the maximum subarray sum for the array [-5, -1, -8, -9] using the divide and conquer approach?
Attempts:
2 left
💡 Hint
The maximum subarray sum is the largest single element when all are negative.
✗ Incorrect
When all numbers are negative, the maximum subarray is the one with the least negative value, which is -1 here.
❓ Predict Output
expertOutput of Maximum Subarray Sum with Mixed Positive and Negative Values
What is the output of this code that finds the maximum subarray sum using divide and conquer for the given array?
DSA Python
def max_crossing_sum(arr, left, mid, right): left_sum = float('-inf') total = 0 for i in range(mid, left - 1, -1): total += arr[i] if total > left_sum: left_sum = total right_sum = float('-inf') total = 0 for i in range(mid + 1, right + 1): total += arr[i] if total > right_sum: right_sum = total return left_sum + right_sum def max_subarray_sum(arr, left, right): if left == right: return arr[left] mid = (left + right) // 2 left_max = max_subarray_sum(arr, left, mid) right_max = max_subarray_sum(arr, mid + 1, right) cross_max = max_crossing_sum(arr, left, mid, right) return max(left_max, right_max, cross_max) arr = [2, -1, 2, 3, 4, -5] print(max_subarray_sum(arr, 0, len(arr) - 1))
Attempts:
2 left
💡 Hint
Consider the subarray that crosses the middle and sums the largest.
✗ Incorrect
The maximum subarray is [2, -1, 2, 3, 4] which sums to 10. The divide and conquer approach finds this by checking left, right, and crossing sums (left suffix max 3 + right prefix max 7 = 10).
