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 C# code that finds the maximum subarray sum using divide and conquer?
DSA C#
using System; class Program { static int MaxCrossingSum(int[] arr, int left, int mid, int right) { int sum = 0; int left_sum = int.MinValue; for (int i = mid; i >= left; i--) { sum += arr[i]; if (sum > left_sum) left_sum = sum; } sum = 0; int right_sum = int.MinValue; for (int i = mid + 1; i <= right; i++) { sum += arr[i]; if (sum > right_sum) right_sum = sum; } return left_sum + right_sum; } static int MaxSubArraySum(int[] arr, int left, int right) { if (left == right) return arr[left]; int mid = (left + right) / 2; int left_max = MaxSubArraySum(arr, left, mid); int right_max = MaxSubArraySum(arr, mid + 1, right); int cross_max = MaxCrossingSum(arr, left, mid, right); return Math.Max(Math.Max(left_max, right_max), cross_max); } static void Main() { int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; int max_sum = MaxSubArraySum(arr, 0, arr.Length - 1); Console.WriteLine(max_sum); } }
Attempts:
2 left
💡 Hint
Think about the subarray with the largest sum in the array.
✗ Incorrect
The maximum subarray is [4, -1, 2, 1] which sums to 6.
❓ Predict Output
intermediateMaximum Subarray Sum for All Negative Numbers
What is the output of this C# code when the input array contains all negative numbers?
DSA C#
using System; class Program { static int MaxCrossingSum(int[] arr, int left, int mid, int right) { int sum = 0; int left_sum = int.MinValue; for (int i = mid; i >= left; i--) { sum += arr[i]; if (sum > left_sum) left_sum = sum; } sum = 0; int right_sum = int.MinValue; for (int i = mid + 1; i <= right; i++) { sum += arr[i]; if (sum > right_sum) right_sum = sum; } return left_sum + right_sum; } static int MaxSubArraySum(int[] arr, int left, int right) { if (left == right) return arr[left]; int mid = (left + right) / 2; int left_max = MaxSubArraySum(arr, left, mid); int right_max = MaxSubArraySum(arr, mid + 1, right); int cross_max = MaxCrossingSum(arr, left, mid, right); return Math.Max(Math.Max(left_max, right_max), cross_max); } static void Main() { int[] arr = {-8, -3, -6, -2, -5, -4}; int max_sum = MaxSubArraySum(arr, 0, arr.Length - 1); Console.WriteLine(max_sum); } }
Attempts:
2 left
💡 Hint
The maximum subarray in all negative numbers is the single largest element.
✗ Incorrect
The largest element is -2, so the maximum subarray sum is -2.
🔧 Debug
advancedIdentify the Bug in MaxCrossingSum Implementation
Which option correctly identifies the bug in this MaxCrossingSum method?
DSA C#
static int MaxCrossingSum(int[] arr, int left, int mid, int right) { int sum = 0; int left_sum = 0; for (int i = mid; i >= left; i--) { sum += arr[i]; if (sum > left_sum) left_sum = sum; } sum = 0; int right_sum = 0; for (int i = mid + 1; i <= right; i++) { sum += arr[i]; if (sum > right_sum) right_sum = sum; } return left_sum + right_sum; }
Attempts:
2 left
💡 Hint
Consider what happens if all numbers are negative.
✗ Incorrect
Initializing left_sum and right_sum to 0 causes the function to ignore negative sums, which is wrong if all numbers are negative.
🧠 Conceptual
advancedTime Complexity of Divide and Conquer Maximum Subarray
What is the time complexity of the divide and conquer algorithm for finding the maximum subarray sum?
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.
🚀 Application
expertFind Maximum Subarray Indices Using Divide and Conquer
Which option correctly modifies the divide and conquer approach to return the start and end indices of the maximum subarray along with the sum?
DSA C#
static (int maxSum, int start, int end) MaxCrossingSum(int[] arr, int left, int mid, int right) { int sum = 0; int left_sum = int.MinValue; int max_left = mid; for (int i = mid; i >= left; i--) { sum += arr[i]; if (sum > left_sum) { left_sum = sum; max_left = i; } } sum = 0; int right_sum = int.MinValue; int max_right = mid + 1; for (int i = mid + 1; i <= right; i++) { sum += arr[i]; if (sum > right_sum) { right_sum = sum; max_right = i; } } return (left_sum + right_sum, max_left, max_right); } static (int maxSum, int start, int end) MaxSubArraySum(int[] arr, int left, int right) { if (left == right) return (arr[left], left, right); int mid = (left + right) / 2; var left_result = MaxSubArraySum(arr, left, mid); var right_result = MaxSubArraySum(arr, mid + 1, right); var cross_result = MaxCrossingSum(arr, left, mid, right); if (left_result.maxSum >= right_result.maxSum && left_result.maxSum >= cross_result.maxSum) return left_result; else if (right_result.maxSum >= left_result.maxSum && right_result.maxSum >= cross_result.maxSum) return right_result; else return cross_result; } static void Main() { int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; var result = MaxSubArraySum(arr, 0, arr.Length - 1); Console.WriteLine($"Sum: {result.maxSum}, Start: {result.start}, End: {result.end}"); }
Attempts:
2 left
💡 Hint
Track indices when calculating left and right sums crossing the midpoint.
✗ Incorrect
The maximum subarray is [4, -1, 2, 1] from index 3 to 6 with sum 6.
