📖 Scenario: You are analyzing daily temperature changes to find the longest period of increasing temperature. This can be represented as finding the maximum sum of a contiguous subarray in an array of integers.
🎯 Goal: Build a program in C that uses the divide and conquer method to find the maximum sum of a contiguous subarray in a given integer array.
📋 What You'll Learn
Create an integer array called arr with the exact values: {2, -4, 3, -1, 5, -6, 1, 4, -2, 3}
Create two integer variables called low and high to represent the start and end indices of the array
Write a function called maxCrossingSum that finds the maximum subarray sum crossing the midpoint
Write a recursive function called maxSubArraySum that uses divide and conquer to find the maximum subarray sum
Print the maximum subarray sum found by calling maxSubArraySum(arr, low, high)
💡 Why This Matters
🌍 Real World
Finding maximum subarrays is useful in financial analysis to find the best time to buy and sell stocks for maximum profit.
💼 Career
Understanding divide and conquer algorithms and recursion is essential for software engineering roles that require problem-solving and optimization skills.
Progress0 / 4 steps
1
Create the integer array
Create an integer array called arr with these exact values: {2, -4, 3, -1, 5, -6, 1, 4, -2, 3}
DSA C
Hint
Use int arr[] = {2, -4, 3, -1, 5, -6, 1, 4, -2, 3}; to create the array.
2
Set the low and high indices
Create two integer variables called low and high. Set low to 0 and high to 9 (the last index of arr).
DSA C
Hint
Set low to 0 and high to 9 because the array has 10 elements indexed 0 to 9.
3
Write the divide and conquer functions
Write the function maxCrossingSum that takes arr, low, mid, and high and returns the maximum sum of a subarray crossing mid. Then write the recursive function maxSubArraySum that takes arr, low, and high and returns the maximum subarray sum using divide and conquer.
DSA C
Hint
Use two loops in maxCrossingSum to find max sums on left and right of mid. Use recursion in maxSubArraySum to divide the array.
4
Print the maximum subarray sum
Write a main function that calls maxSubArraySum(arr, low, high) and prints the result with printf.
DSA C
Hint
Call maxSubArraySum(arr, low, high) and print the result using printf.
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
Step 1: Understand divide and conquer strategy
The array is split into two halves recursively to simplify the problem.
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.
Final Answer:
Divide the array into two halves, find max subarray in left, right, and crossing middle, then combine results -> Option C
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
Step 1: Identify base case meaning
When the subarray has only one element (low equals high), the max subarray sum is that element itself.
Step 2: Match base case condition
The condition if (low == high) return arr[low]; correctly returns the single element's value.
Final Answer:
if (low == high) return arr[low]; -> Option D
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
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.
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.
Final Answer:
6 -> Option A
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
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.
Step 2: Correct initialization
They should be initialized to the smallest integer (INT_MIN) to correctly track max sums even if negative.
Final Answer:
left_sum and right_sum should be initialized to INT_MIN, not 0 -> Option A
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
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).
Step 2: Identify max element
The largest element is -1, so max subarray sum is -1 (single element subarray).
Final Answer:
-1 because the max subarray is the single largest element -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.