Challenge - 5 Problems
Maximum Subarray Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Maximum Subarray Sum Using Divide and Conquer
What is the output of the following TypeScript code that finds the maximum subarray sum using divide and conquer?
DSA Typescript
function maxCrossingSum(arr: number[], left: number, mid: number, right: number): number {
let sum = 0;
let leftSum = Number.NEGATIVE_INFINITY;
for (let i = mid; i >= left; i--) {
sum += arr[i];
if (sum > leftSum) leftSum = sum;
}
sum = 0;
let rightSum = Number.NEGATIVE_INFINITY;
for (let i = mid + 1; i <= right; i++) {
sum += arr[i];
if (sum > rightSum) rightSum = sum;
}
return leftSum + rightSum;
}
function maxSubArraySum(arr: number[], left: number, right: number): number {
if (left === right) return arr[left];
const mid = Math.floor((left + right) / 2);
const leftMax = maxSubArraySum(arr, left, mid);
const rightMax = maxSubArraySum(arr, mid + 1, right);
const crossMax = maxCrossingSum(arr, left, mid, right);
return Math.max(leftMax, rightMax, crossMax);
}
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 that crosses the middle point.
✗ Incorrect
The maximum subarray is [4, -1, 2, 1] which sums to 6.
🧠 Conceptual
intermediate1:30remaining
Understanding the Divide and Conquer Approach for Maximum Subarray
In the divide and conquer method for finding the maximum subarray, which of the following best describes the role of the 'crossing sum'?
Attempts:
2 left
💡 Hint
Think about subarrays that include elements from both halves.
✗ Incorrect
The crossing sum specifically looks for the maximum subarray that includes elements from both sides of the midpoint.
🔧 Debug
advanced2:00remaining
Identify the Error in Maximum Subarray Divide and Conquer Code
What error will the following TypeScript code produce when executed?
DSA Typescript
function maxCrossingSum(arr: number[], left: number, mid: number, right: number): number {
let sum = 0;
let leftSum = Number.NEGATIVE_INFINITY;
for (let i = mid; i >= left; i--) {
sum += arr[i];
if (sum > leftSum) leftSum = sum;
}
sum = 0;
let rightSum = Number.NEGATIVE_INFINITY;
for (let i = mid + 1; i <= right; i++) {
sum += arr[i];
if (sum > rightSum) rightSum = sum;
}
return leftSum + rightSum;
}
function maxSubArraySum(arr: number[], left: number, right: number): number {
if (left === right) return arr[left];
const mid = Math.floor((left + right) / 2);
const leftMax = maxSubArraySum(arr, left, mid);
const rightMax = maxSubArraySum(arr, mid + 1, right);
const crossMax = maxCrossingSum(arr, left, mid, right);
return Math.max(leftMax, rightMax, crossMax);
}
const arr = [];
console.log(maxSubArraySum(arr, 0, arr.length - 1));Attempts:
2 left
💡 Hint
Consider what happens when the input array is empty.
✗ Incorrect
The recursive function calls itself with invalid indices causing infinite recursion and stack overflow.
❓ Predict Output
advanced2:00remaining
Resulting Maximum Subarray Sum for Negative Numbers
What is the output of the following code that finds the maximum subarray sum using divide and conquer on an array of all negative numbers?
DSA Typescript
const arr = [-8, -3, -6, -2, -5, -4]; function maxCrossingSum(arr: number[], left: number, mid: number, right: number): number { let sum = 0; let leftSum = Number.NEGATIVE_INFINITY; for (let i = mid; i >= left; i--) { sum += arr[i]; if (sum > leftSum) leftSum = sum; } sum = 0; let rightSum = Number.NEGATIVE_INFINITY; for (let i = mid + 1; i <= right; i++) { sum += arr[i]; if (sum > rightSum) rightSum = sum; } return leftSum + rightSum; } function maxSubArraySum(arr: number[], left: number, right: number): number { if (left === right) return arr[left]; const mid = Math.floor((left + right) / 2); const leftMax = maxSubArraySum(arr, left, mid); const rightMax = maxSubArraySum(arr, mid + 1, right); const crossMax = maxCrossingSum(arr, left, mid, right); return Math.max(leftMax, rightMax, crossMax); } console.log(maxSubArraySum(arr, 0, arr.length - 1));
Attempts:
2 left
💡 Hint
The maximum subarray in all negative numbers is the least negative single element.
✗ Incorrect
The maximum subarray is [-2] with sum -2, which is the largest among all negative numbers.
🚀 Application
expert3:00remaining
Maximum Subarray Sum with Indices Using Divide and Conquer
Given the divide and conquer approach, which option correctly returns the maximum subarray sum along with the start and end indices of that subarray?
DSA Typescript
function maxCrossingSum(arr: number[], left: number, mid: number, right: number): {maxSum: number, start: number, end: number} {
let sum = 0;
let leftSum = Number.NEGATIVE_INFINITY;
let maxLeft = mid;
for (let i = mid; i >= left; i--) {
sum += arr[i];
if (sum > leftSum) {
leftSum = sum;
maxLeft = i;
}
}
sum = 0;
let rightSum = Number.NEGATIVE_INFINITY;
let maxRight = mid + 1;
for (let i = mid + 1; i <= right; i++) {
sum += arr[i];
if (sum > rightSum) {
rightSum = sum;
maxRight = i;
}
}
return {maxSum: leftSum + rightSum, start: maxLeft, end: maxRight};
}
function maxSubArraySum(arr: number[], left: number, right: number): {maxSum: number, start: number, end: number} {
if (left === right) return {maxSum: arr[left], start: left, end: right};
const mid = Math.floor((left + right) / 2);
const leftResult = maxSubArraySum(arr, left, mid);
const rightResult = maxSubArraySum(arr, mid + 1, right);
const crossResult = maxCrossingSum(arr, left, mid, right);
if (leftResult.maxSum >= rightResult.maxSum && leftResult.maxSum >= crossResult.maxSum) return leftResult;
else if (rightResult.maxSum >= leftResult.maxSum && rightResult.maxSum >= crossResult.maxSum) return rightResult;
else return crossResult;
}
const arr = [13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7];
console.log(maxSubArraySum(arr, 0, arr.length - 1));Attempts:
2 left
💡 Hint
Check the subarray with the largest sum and its indices carefully.
✗ Incorrect
The maximum subarray is [18, 20, -7, 12] from index 7 to 10 with sum 43.