0
0
DSA Cprogramming~20 mins

Find Maximum Subarray Divide and Conquer in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Maximum Subarray Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Divide and Conquer Maximum Subarray Sum
What is the output of the following C code that finds the maximum subarray sum using divide and conquer?
DSA C
int max(int a, int b) { return (a > b) ? a : b; }

int maxCrossingSum(int arr[], int l, int m, int h) {
    int sum = 0;
    int left_sum = -2147483648;
    for (int i = m; i >= l; i--) {
        sum += arr[i];
        if (sum > left_sum) left_sum = sum;
    }
    sum = 0;
    int right_sum = -2147483648;
    for (int i = m + 1; i <= h; i++) {
        sum += arr[i];
        if (sum > right_sum) right_sum = sum;
    }
    return left_sum + right_sum;
}

int maxSubArraySum(int arr[], int l, int h) {
    if (l == h) return arr[l];
    int m = (l + h) / 2;
    int left_max = maxSubArraySum(arr, l, m);
    int right_max = maxSubArraySum(arr, m + 1, h);
    int cross_max = maxCrossingSum(arr, l, m, h);
    return max(max(left_max, right_max), cross_max);
}

#include <stdio.h>
int main() {
    int arr[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
    int n = sizeof(arr) / sizeof(arr[0]);
    int max_sum = maxSubArraySum(arr, 0, n - 1);
    printf("%d", max_sum);
    return 0;
}
A6
B7
C5
D4
Attempts:
2 left
💡 Hint
Think about the subarray with the largest sum in the array.
🧠 Conceptual
intermediate
1:30remaining
Key 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?
ASort the array and pick the largest element as the maximum subarray sum.
BUse a sliding window to find the maximum sum subarray in one pass.
CDivide the array into two halves, find maximum subarray in left, right, and crossing middle, then take the maximum.
DCalculate all possible subarrays and pick the one with the maximum sum.
Attempts:
2 left
💡 Hint
Think about how the problem is split into smaller parts.
🔧 Debug
advanced
2:00remaining
Identify the Bug in maxCrossingSum Function
What is the bug in this maxCrossingSum function snippet?
DSA C
int maxCrossingSum(int arr[], int l, int m, int h) {
    int sum = 0;
    int left_sum = 0;
    for (int i = m; i >= l; i--) {
        sum += arr[i];
        if (sum > left_sum) left_sum = sum;
    }
    sum = 0;
    int right_sum = 0;
    for (int i = m + 1; i <= h; i++) {
        sum += arr[i];
        if (sum > right_sum) right_sum = sum;
    }
    return left_sum + right_sum;
}
Aleft_sum and right_sum initialized to 0 cause incorrect results when all numbers are negative.
BThe loops should run from l to m and m+1 to h instead of backwards.
Csum variable should be declared inside the loops to reset automatically.
DThe function should return max(left_sum, right_sum) instead of their sum.
Attempts:
2 left
💡 Hint
Consider what happens if all elements are negative.
Predict Output
advanced
1:30remaining
Output of maxSubArraySum on All Negative Array
What is the output of the following code snippet?
DSA C
int arr[] = {-8, -3, -6, -2, -5, -4};
int n = sizeof(arr) / sizeof(arr[0]);
int max_sum = maxSubArraySum(arr, 0, n - 1);
printf("%d", max_sum);
A-8
B-3
C0
D-2
Attempts:
2 left
💡 Hint
The maximum subarray in all negative numbers is the least negative single element.
🚀 Application
expert
1:30remaining
Time 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?
AO(n^2)
BO(n log n)
CO(n)
DO(log n)
Attempts:
2 left
💡 Hint
Consider the recurrence relation T(n) = 2T(n/2) + O(n).