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 Go code that finds the maximum subarray sum using divide and conquer?
DSA Go
package main import "fmt" func max(a, b int) int { if a > b { return a } return b } func maxCrossingSum(arr []int, left, mid, right int) int { sum := 0 left_sum := -1 << 31 for i := mid; i >= left; i-- { sum += arr[i] if sum > left_sum { left_sum = sum } } sum = 0 right_sum := -1 << 31 for i := mid + 1; i <= right; i++ { sum += arr[i] if sum > right_sum { right_sum = sum } } return max(left_sum+right_sum, max(left_sum, right_sum)) } func maxSubArraySum(arr []int, left, right int) int { if left == right { return arr[left] } mid := (left + right) / 2 left_max := maxSubArraySum(arr, left, mid) right_max := maxSubArraySum(arr, mid+1, right) cross_max := maxCrossingSum(arr, left, mid, right) return max(max(left_max, right_max), cross_max) } func main() { arr := []int{-2, 1, -3, 4, -1, 2, 1, -5, 4} result := maxSubArraySum(arr, 0, len(arr)-1) fmt.Println(result) }
Attempts:
2 left
💡 Hint
Think about the subarray with the largest sum in the given array.
✗ Incorrect
The maximum subarray is [4, -1, 2, 1] which sums to 6.
🧠 Conceptual
intermediateKey Idea Behind Divide and Conquer for Maximum Subarray
Which statement best describes the key idea behind the divide and conquer approach to find the maximum subarray?
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, right, and crossing subarrays, then combines results to find the maximum.
🔧 Debug
advancedIdentify the Bug in maxCrossingSum Function
What error will occur if the initial values of left_sum and right_sum in maxCrossingSum are set to 0 instead of a very small number?
DSA Go
func maxCrossingSum(arr []int, left, mid, right int) int { sum := 0 left_sum := 0 for i := mid; i >= left; i-- { sum += arr[i] if sum > left_sum { left_sum = sum } } sum = 0 right_sum := 0 for i := mid + 1; i <= right; i++ { sum += arr[i] if sum > right_sum { right_sum = sum } } return max(left_sum+right_sum, max(left_sum, right_sum)) }
Attempts:
2 left
💡 Hint
Consider what happens if all elements are negative.
✗ Incorrect
Initializing left_sum and right_sum to 0 causes the function to ignore negative sums, leading to wrong results if all numbers are negative.
❓ Predict Output
advancedOutput of maxSubArraySum for All Negative Array
What is the output of maxSubArraySum when the input array is all negative numbers?
DSA Go
package main import "fmt" func max(a, b int) int { if a > b { return a } return b } func maxCrossingSum(arr []int, left, mid, right int) int { sum := 0 left_sum := -1 << 31 for i := mid; i >= left; i-- { sum += arr[i] if sum > left_sum { left_sum = sum } } sum = 0 right_sum := -1 << 31 for i := mid + 1; i <= right; i++ { sum += arr[i] if sum > right_sum { right_sum = sum } } return max(left_sum+right_sum, max(left_sum, right_sum)) } func maxSubArraySum(arr []int, left, right int) int { if left == right { return arr[left] } mid := (left + right) / 2 left_max := maxSubArraySum(arr, left, mid) right_max := maxSubArraySum(arr, mid+1, right) cross_max := maxCrossingSum(arr, left, mid, right) return max(max(left_max, right_max), cross_max) } func main() { arr := []int{-8, -3, -6, -2, -5, -4} result := maxSubArraySum(arr, 0, len(arr)-1) fmt.Println(result) }
Attempts:
2 left
💡 Hint
The maximum subarray in all negative numbers is the least negative number.
✗ Incorrect
The maximum subarray sum is -2, which is the largest single element in the array.
🚀 Application
expertTime Complexity of Divide and Conquer Maximum Subarray
What is the time complexity of the divide and conquer algorithm for finding the maximum subarray sum for an array of size n?
Attempts:
2 left
💡 Hint
Consider the recurrence relation T(n) = 2T(n/2) + O(n).
✗ Incorrect
The algorithm divides the array into halves recursively and combines results in linear time, leading to O(n log n) complexity.
