Bird
0
0
DSA Cprogramming~10 mins

Maximum Product Subarray in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Maximum Product Subarray
Start at first element
Initialize max_prod, min_prod, result
For each element in array
Calculate candidates: current element, max_prod*element, min_prod*element
Update max_prod = max(candidates)
Update min_prod = min(candidates)
Update result = max(result, max_prod)
Repeat for all elements
Return result
We move through the array, keeping track of the maximum and minimum products ending at each position, updating the overall maximum product found.
Execution Sample
DSA C
int maxProduct(int* nums, int numsSize) {
    int max_prod = nums[0], min_prod = nums[0], result = nums[0];
    for (int i = 1; i < numsSize; i++) {
        int candidates[3] = {nums[i], max_prod * nums[i], min_prod * nums[i]};
        max_prod = candidates[0] > candidates[1] ? candidates[0] : candidates[1];
        max_prod = max_prod > candidates[2] ? max_prod : candidates[2];
        min_prod = candidates[0] < candidates[1] ? candidates[0] : candidates[1];
        min_prod = min_prod < candidates[2] ? min_prod : candidates[2];
        if (max_prod > result) result = max_prod;
    }
    return result;
}
This code finds the maximum product of any contiguous subarray by tracking max and min products at each step.
Execution Table
StepOperationCurrent ElementCandidates (curr, max_prod*curr, min_prod*curr)max_prodmin_prodresultVisual State
0Initialize2[2, -, -]222Array: [2,3,-2,4]
1Process element3[3, 2*3=6, 2*3=6]636Max product subarray ends at index 1: [2,3]
2Process element-2[-2, 6*(-2)=-12, 3*(-2)=-6]6-126Max product subarray still [2,3], min_prod updated
3Process element4[4, 6*4=24, -12*4=-48]24-4824Max product subarray updated to [2,3,-2,4]
4End----24Final max product is 24
💡 All elements processed, maximum product subarray found
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
max_prod2662424
min_prod23-12-48-48
result2662424
Key Moments - 3 Insights
Why do we track both max_prod and min_prod at each step?
Because a negative number can turn a small min_prod into a large max_prod when multiplied, tracking both ensures we don't miss a larger product (see execution_table steps 2 and 3).
Why do we update result only with max_prod and not min_prod?
Result tracks the maximum product found so far, which must be the max_prod at each step, since min_prod is the smallest product and can't be the maximum (see execution_table result column).
What happens when the current element is negative?
max_prod and min_prod swap roles because multiplying by a negative flips signs, so we calculate candidates and pick max and min accordingly (see step 2 and 3 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of min_prod?
A-6
B-12
C3
D-2
💡 Hint
Check the 'min_prod' column at step 2 in execution_table
At which step does the result get updated to 6?
AStep 0
BStep 1
CStep 2
DStep 3
💡 Hint
Look at the 'result' column in execution_table and see when it changes from 2 to 6
If the array started with a negative number, how would max_prod and min_prod initialization change?
ABoth start as 0
Bmax_prod starts as 0, min_prod as negative number
CBoth start as the first element, even if negative
Dmax_prod starts as 1, min_prod as -1
💡 Hint
Check variable_tracker 'Start' values and initialization in code
Concept Snapshot
Maximum Product Subarray:
- Track max_prod and min_prod at each element
- max_prod = max(current, max_prod*current, min_prod*current)
- min_prod = min(current, max_prod*current, min_prod*current)
- Update result with max_prod
- Handles negative numbers by tracking min_prod
- Returns max product of any contiguous subarray
Full Transcript
The Maximum Product Subarray problem finds the largest product of contiguous elements in an array. We keep track of two values at each step: max_prod, the maximum product ending at the current element, and min_prod, the minimum product ending at the current element. This is important because multiplying by a negative number can turn a small min_prod into a large max_prod. We update these values by considering the current element alone, or multiplied by the previous max_prod or min_prod. The overall result is updated with the max_prod at each step. This approach ensures we find the maximum product subarray even when negative numbers are involved.