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 JavaScript code that finds the maximum subarray sum using divide and conquer?
DSA Javascript
function maxCrossingSum(arr, left, mid, right) {
let sum = 0;
let left_sum = -Infinity;
for (let i = mid; i >= left; i--) {
sum += arr[i];
if (sum > left_sum) left_sum = sum;
}
sum = 0;
let right_sum = -Infinity;
for (let i = mid + 1; i <= right; i++) {
sum += arr[i];
if (sum > right_sum) right_sum = sum;
}
return left_sum + right_sum;
}
function maxSubArraySum(arr, left, right) {
if (left === right) return arr[left];
let mid = Math.floor((left + right) / 2);
let left_max = maxSubArraySum(arr, left, mid);
let right_max = maxSubArraySum(arr, mid + 1, right);
let cross_max = maxCrossingSum(arr, left, mid, right);
return Math.max(left_max, right_max, cross_max);
}
const arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
console.log(maxSubArraySum(arr, 0, arr.length - 1));Attempts:
2 left
💡 Hint
Think about the subarray with the largest sum in the array [-2, 1, -3, 4, -1, 2, 1, -5, 4].
✗ 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 recursively.
🧠 Conceptual
intermediateUnderstanding the Divide and Conquer Approach for Maximum Subarray
Which of the following best describes the role of the 'maxCrossingSum' function in the divide and conquer algorithm for finding the maximum subarray?
Attempts:
2 left
💡 Hint
Consider what happens when the maximum subarray crosses the midpoint.
✗ Incorrect
The 'maxCrossingSum' function calculates the maximum sum of a subarray that crosses the midpoint, combining elements from both halves.
🔧 Debug
advancedIdentify the Error in Maximum Subarray Divide and Conquer Implementation
What error will occur when running the following code snippet for maximum subarray sum using divide and conquer?
DSA Javascript
function maxCrossingSum(arr, left, mid, right) {
let sum = 0;
let left_sum = -Infinity;
for (let i = mid; i >= left; i--) {
sum += arr[i];
if (sum > left_sum) left_sum = sum;
}
sum = 0;
let right_sum = -Infinity;
for (let i = mid + 1; i <= right; i++) {
sum += arr[i];
if (sum > right_sum) right_sum = sum;
}
return left_sum + right_sum;
}
function maxSubArraySum(arr, left, right) {
if (left > right) return 0;
if (left === right) return arr[left];
let mid = Math.floor((left + right) / 2);
let left_max = maxSubArraySum(arr, left, mid);
let right_max = maxSubArraySum(arr, mid + 1, right);
let cross_max = maxCrossingSum(arr, left, mid, right);
return Math.max(left_max, right_max, cross_max);
}
const arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4];
console.log(maxSubArraySum(arr, 0, arr.length - 1));Attempts:
2 left
💡 Hint
Think about what happens when all numbers are negative and the base case returns 0.
✗ Incorrect
Returning 0 when left > right causes the algorithm to consider empty subarrays, which can lead to incorrect maximum sums when all elements are negative.
📝 Syntax
advancedSyntax Error in Maximum Subarray Divide and Conquer Code
Which option contains a syntax error that will prevent the maximum subarray divide and conquer code from running?
Attempts:
2 left
💡 Hint
Look carefully for missing semicolons or syntax mistakes inside loops.
✗ Incorrect
Option B is missing a semicolon after 'let sum = 0' which causes a syntax error in JavaScript.
🚀 Application
expertTime Complexity of Maximum Subarray Divide and Conquer Algorithm
What is the time complexity of the divide and conquer algorithm for finding the maximum subarray sum in an array of size n?
Attempts:
2 left
💡 Hint
Consider how the array is split and how the crossing sum is calculated at each level.
✗ Incorrect
The algorithm splits the array into two halves recursively (log n levels) and computes crossing sums in linear time at each level, resulting in O(n log n) time complexity.
