Bird
Raised Fist0
DSA Cprogramming~10 mins

Find Maximum Subarray Divide and Conquer in DSA C - Execution Trace

Choose your learning style10 modes available
Concept Flow - Find Maximum Subarray Divide and Conquer
Start with full array
↓
Divide array into two halves
↓
Find max subarray in left half
↓
Find max crossing subarray
↓
Compare left, right, crossing max
↓
Return max subarray
↓
Done
Divide the array into halves, find max subarray in left, right, and crossing middle, then return the max of these three.
Execution Sample
DSA C
int maxCrossingSum(int arr[], int left, int mid, int right) {
  int sum = 0, left_sum = INT_MIN;
  for (int i = mid; i >= left; i--) {
    sum += arr[i];
    if (sum > left_sum) left_sum = sum;
  }
  sum = 0; int right_sum = INT_MIN;
  for (int i = mid + 1; i <= right; i++) {
    sum += arr[i];
    if (sum > right_sum) right_sum = sum;
  }
  return left_sum + right_sum;
}
This function finds the maximum subarray sum that crosses the middle point.
Execution Table
StepOperationSubarray RangeMax Left SumMax Right SumCross SumMax Subarray SumVisual State
1Divide full array[0..7]----[2, -1, 3, 4, -2, 1, 5, -3]
2Divide left half[0..3]----[2, -1, 3, 4]
3Divide left-left half[0..1]----[2, -1]
4Base case single element[0..0]2--2[2]
5Base case single element[1..1]-1---1[-1]
6Max crossing sum [0..1][0..1]2-112[2, -1]
7Max left half max[0..1]2-112[2, -1]
8Divide left-right half[2..3]----[3, 4]
9Base case single element[2..2]3--3[3]
10Base case single element[3..3]4--4[4]
11Max crossing sum [2..3][2..3]3477[3, 4]
12Max left half max[0..3]2788[2, -1, 3, 4]
13Divide right half[4..7]----[-2, 1, 5, -3]
14Divide right-left half[4..5]----[-2, 1]
15Base case single element[4..4]-2---2[-2]
16Base case single element[5..5]1--1[1]
17Max crossing sum [4..5][4..5]-21-11[-2, 1]
18Divide right-right half[6..7]----[5, -3]
19Base case single element[6..6]5--5[5]
20Base case single element[7..7]-3---3[-3]
21Max crossing sum [6..7][6..7]5-325[5, -3]
22Max right half max[4..7]1566[-2, 1, 5, -3]
23Max crossing sum full [0..7][0..7]861212[2, -1, 3, 4, -2, 1, 5, -3]
24Final max subarray sum[0..7]861212[2, -1, 3, 4, -2, 1, 5, -3]
💡 All subproblems solved, final max subarray sum is 12 for subarray [2, -1, 3, 4, -2, 1, 5]
Variable Tracker
VariableStartAfter Step 6After Step 11After Step 12After Step 17After Step 21After Step 22After Step 23Final
maxLeftSum-27815688
maxRightSum--1471-3566
crossSum-178-1261212
maxSubarraySum-2781561212
Key Moments - 3 Insights
Why do we calculate max crossing sum separately?
Because the maximum subarray might cross the middle point, so we must check the sum crossing left and right halves (see steps 6, 11, 17, 21, 23 in execution_table).
Why do we return the maximum of left, right, and crossing sums?
Because the max subarray can be fully in the left half, fully in the right half, or crossing the middle. We compare all three to find the overall max (see step 23).
What happens at base cases with single elements?
At base cases (steps 4,5,9,10,15,16,19,20), the max subarray is the element itself, so we return that element's value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 23, what is the max crossing sum for the full array?
A6
B12
C8
D7
💡 Hint
Check the 'Cross Sum' column at step 23 in execution_table.
At which step does the max subarray sum first become 7?
AStep 6
BStep 17
CStep 11
DStep 21
💡 Hint
Look at the 'Max Subarray Sum' column in execution_table and find when it first reaches 7.
If the array had all negative numbers, how would the base case steps change?
ABase cases would return the least negative element as max subarray.
BBase cases would return zero.
CBase cases would return the sum of all elements.
DBase cases would return positive infinity.
💡 Hint
Recall base cases return the single element value, so max subarray is the largest single element (see steps 4,5,9,10,15,16,19,20).
Concept Snapshot
Divide and Conquer approach to find max subarray:
- Divide array into halves
- Recursively find max subarray in left and right halves
- Find max crossing subarray crossing middle
- Return max of left, right, crossing sums
- Base case: single element subarray
- Time complexity: O(n log n)
Full Transcript
This visualization shows the divide and conquer method to find the maximum subarray sum. The array is split into halves recursively until single elements are reached. At each step, the maximum subarray sum is found for the left half, right half, and the subarray crossing the middle. The maximum of these three is returned. The execution table tracks these steps with subarray ranges and sums. The variable tracker shows how max sums change. Key moments clarify why crossing sums are needed and how base cases work. The quiz tests understanding of these steps and results.

Practice

(1/5)
1. What is the main idea behind the Divide and Conquer approach to find the maximum subarray sum?
easy
A. Use a sliding window to find the maximum sum subarray in one pass
B. Sort the array and pick the largest element as max subarray sum
C. Divide the array into two halves, find max subarray in left, right, and crossing middle, then combine results
D. Calculate sum of all subarrays and pick the maximum

Solution

  1. Step 1: Understand divide and conquer strategy

    The array is split into two halves recursively to simplify the problem.
  2. Step 2: Combine results from left, right, and crossing subarrays

    We find max subarray sums in left half, right half, and subarrays crossing the middle, then take the maximum of these.
  3. Final Answer:

    Divide the array into two halves, find max subarray in left, right, and crossing middle, then combine results -> Option C
  4. Quick Check:

    Divide and conquer = split, solve, combine [OK]
Hint: Split array, solve halves, check crossing sum, combine [OK]
Common Mistakes:
  • Thinking sorting helps find max subarray sum
  • Ignoring subarrays crossing the middle
  • Trying to sum all subarrays directly
2. Which of the following is the correct base case condition in the divide and conquer function for maximum subarray?
easy
A. if (low == high) return 0;
B. if (low > high) return 0;
C. if (low < high) return arr[low] + arr[high];
D. if (low == high) return arr[low];

Solution

  1. Step 1: Identify base case meaning

    When the subarray has only one element (low equals high), the max subarray sum is that element itself.
  2. Step 2: Match base case condition

    The condition if (low == high) return arr[low]; correctly returns the single element's value.
  3. Final Answer:

    if (low == high) return arr[low]; -> Option D
  4. Quick Check:

    Single element subarray returns element value [OK]
Hint: Base case: one element means return that element [OK]
Common Mistakes:
  • Returning 0 when low == high
  • Using incorrect comparison operators
  • Returning sum of two elements instead of one
3. What is the output of the following C code snippet using divide and conquer to find max subarray sum?
int arr[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4};
int result = maxSubArray(arr, 0, 8);
printf("%d", result);
Assume maxSubArray is correctly implemented divide and conquer function.
medium
A. 6
B. 7
C. 5
D. 4

Solution

  1. Step 1: Identify max subarray in given array

    The maximum subarray is [4, -1, 2, 1] with sum 4 + (-1) + 2 + 1 = 6, but check if including more elements helps.
  2. Step 2: Check if extending subarray increases sum

    Including the element before 4 is -3 (reduces sum), after 1 is -5 (reduces sum), so max sum is 6.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Max subarray sum = 6 [OK]
Hint: Find max sum of continuous elements [OK]
Common Mistakes:
  • Counting non-continuous elements
  • Miscomputing sums with negative numbers
  • Assuming max element alone is answer
4. Identify the bug in this divide and conquer max subarray function snippet:
int maxCrossingSum(int arr[], int low, int mid, int high) {
    int left_sum = 0, sum = 0;
    for (int i = mid; i >= low; i--) {
        sum += arr[i];
        if (sum > left_sum)
            left_sum = sum;
    }
    int right_sum = 0;
    sum = 0;
    for (int i = mid + 1; i <= high; i++) {
        sum += arr[i];
        if (sum > right_sum)
            right_sum = sum;
    }
    return left_sum + right_sum;
}
medium
A. left_sum and right_sum should be initialized to INT_MIN, not 0
B. The loops should start from low and high, not mid
C. sum variable should not be reset between loops
D. Return statement should subtract left_sum from right_sum

Solution

  1. Step 1: Understand initialization of sums

    Initializing left_sum and right_sum to 0 causes errors if all numbers are negative, as max sum could be less than 0.
  2. Step 2: Correct initialization

    They should be initialized to the smallest integer (INT_MIN) to correctly track max sums even if negative.
  3. Final Answer:

    left_sum and right_sum should be initialized to INT_MIN, not 0 -> Option A
  4. Quick Check:

    Initialize max sums to INT_MIN to handle negatives [OK]
Hint: Use INT_MIN for max sums to handle negative arrays [OK]
Common Mistakes:
  • Initializing max sums to zero causes wrong results
  • Not resetting sum variable properly
  • Incorrect loop ranges
5. Given an array arr = [-1, -2, -3, -4], what will the divide and conquer max subarray function return and why?
hard
A. 0 because empty subarray sum is considered
B. -1 because the max subarray is the single largest element
C. -10 because sum of all elements is max
D. -4 because last element is max

Solution

  1. Step 1: Analyze array with all negative numbers

    All elements are negative, so the max subarray is the one with the least negative value (largest number).
  2. Step 2: Identify max element

    The largest element is -1, so max subarray sum is -1 (single element subarray).
  3. Final Answer:

    -1 because the max subarray is the single largest element -> Option B
  4. Quick Check:

    Max subarray in negatives = max single element [OK]
Hint: Max subarray in all negatives is max single element [OK]
Common Mistakes:
  • Assuming sum of all negatives is max
  • Considering empty subarray sum as zero incorrectly
  • Picking last element without comparison
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard