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 Java code that finds the maximum subarray sum using divide and conquer?
DSA Java
public class MaxSubarray { public static int maxCrossingSum(int[] arr, int left, int mid, int right) { int sum = 0; int left_sum = Integer.MIN_VALUE; for (int i = mid; i >= left; i--) { sum += arr[i]; if (sum > left_sum) left_sum = sum; } sum = 0; int right_sum = Integer.MIN_VALUE; for (int i = mid + 1; i <= right; i++) { sum += arr[i]; if (sum > right_sum) right_sum = sum; } return left_sum + right_sum; } public 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); } public static void main(String[] args) { int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; System.out.println(maxSubArraySum(arr, 0, arr.length - 1)); } }
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. The divide and conquer approach correctly finds this sum.
🧠 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 and right halves, and also considers subarrays crossing the middle, then returns the maximum of these.
🔧 Debug
advancedIdentify the Bug in maxCrossingSum Implementation
What is the bug in the following maxCrossingSum method that causes incorrect maximum crossing sum calculation?
DSA Java
public 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 arrays with all negative numbers.
✗ Incorrect
Initializing left_sum and right_sum to 0 causes incorrect results when all numbers are negative because 0 is greater than any negative sum. Using Integer.MIN_VALUE ensures correct max sums.
❓ Predict Output
advancedOutput of Maximum Subarray Sum for All Negative Array
What is the output of the following code when the input array contains all negative numbers?
DSA Java
public class MaxSubarray { public static int maxCrossingSum(int[] arr, int left, int mid, int right) { int sum = 0; int left_sum = Integer.MIN_VALUE; for (int i = mid; i >= left; i--) { sum += arr[i]; if (sum > left_sum) left_sum = sum; } sum = 0; int right_sum = Integer.MIN_VALUE; for (int i = mid + 1; i <= right; i++) { sum += arr[i]; if (sum > right_sum) right_sum = sum; } return left_sum + right_sum; } public 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); } public static void main(String[] args) { int[] arr = {-8, -3, -6, -2, -5, -4}; System.out.println(maxSubArraySum(arr, 0, arr.length - 1)); } }
Attempts:
2 left
💡 Hint
The maximum subarray in all negative numbers is the least negative number.
✗ Incorrect
The maximum subarray is the single element -2, which is the largest (least negative) number in the array.
🚀 Application
expertTime Complexity of Divide and Conquer Maximum Subarray Algorithm
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 two halves recursively (2T(n/2)) and combines results in linear time O(n), resulting in O(n log n) time complexity.
