Given: A = [2, 3, -2, 4, -1, 0, -3, 5, -4, 2]
We'll use two DP arrays:
- dpMax[i]: Stores the maximum product ending at index i.
- dpMin[i]: Stores the minimum product ending at index i.
- overallMax: Stores the overall maximum product found so far.
Initialization:
- dpMax[0] = 2
- dpMin[0] = 2
- overallMax = 2
Iteration:
Initial State (before any iteration, or after processing A[0]):
A = [2, 3, -2, 4, -1, 0, -3, 5, -4, 2]
dpMax = [2, _, _, _, _, _, _, _, _, _]
dpMin = [2, _, _, _, _, _, _, _, _, _]
overallMax = 2
Iteration 1 (Processing A[1] = 3):
dpMax = [2, 6, _, _, _, _, _, _, _, _]
dpMin = [2, 3, _, _, _, _, _, _, _, _]
overallMax = 6
Iteration 2 (Processing A[2] = -2):
dpMax = [2, 6, -2, _, _, _, _, _, _, _]
dpMin = [2, 3, -12, _, _, _, _, _, _, _]
overallMax = 6
Iteration 3 (Processing A[3] = 4):
dpMax = [2, 6, -2, 4, _, _, _, _, _, _]
dpMin = [2, 3, -12, -48, _, _, _, _, _, _]
overallMax = 6
Iteration 4 (Processing A[4] = -1):
dpMax = [2, 6, -2, 4, 48, _, _, _, _, _]
dpMin = [2, 3, -12, -48, -4, _, _, _, _, _]
overallMax = 48
Iteration 5 (Processing A[5] = 0):
dpMax = [2, 6, -2, 4, 48, 0, _, _, _, _]
dpMin = [2, 3, -12, -48, -4, 0, _, _, _, _]
overallMax = 48
Iteration 6 (Processing A[6] = -3):
dpMax = [2, 6, -2, 4, 48, 0, 0, _, _, _]
dpMin = [2, 3, -12, -48, -4, 0, -3, _, _, _]
overallMax = 48
Iteration 7 (Processing A[7] = 5):
dpMax = [2, 6, -2, 4, 48, 0, 0, 5, _, _]
dpMin = [2, 3, -12, -48, -4, 0, -3, -15, _, _]
overallMax = 48
Iteration 8 (Processing A[8] = -4):
dpMax = [2, 6, -2, 4, 48, 0, 0, 5, 60, _]
dpMin = [2, 3, -12, -48, -4, 0, -3, -15, -20, _]
overallMax = 60
Iteration 9 (Processing A[9] = 2 - Final Iteration):
dpMax = [2, 6, -2, 4, 48, 0, 0, 5, 60, 120]
dpMin = [2, 3, -12, -48, -4, 0, -3, -15, -20, -40]
overallMax = 120