Didnt understand the recurrance

Here in the solution, bhaiya wrote this line in for loop of solution

ans = max(ans, a[left]*a[right]*a[pivot] + Solve(left, pivot) + Solve(right, pivot))

But in the start we are passing left as 0 and right as n+1

So for example n = 5 and a = {4,5,3,6}

so bhaiya first appended 1 at both sides so our array becomes - {1, 4, 5, 3, 6, 1}

and he called on the recurrsion function for the array.

So when pivot is for example = 2 and a[pivot] = 5

bhaiya multiplied a[left] with a[right] and with a[pivot]

But my point is left and right for pivot = 2 is (pivot-1) and (pivot+1). so why bhaiya multiplied with a[left[ and a[right] ???

As in question it is written that we have to multiply with immediate left and immediate right elements for any ith element.

a[left] represents a[pivot-1] and a[right] represents a[pivot+1] only

No. he left and right are not pivot-1 and pivot+1

when in main() he calls Solve function, he passes left as 0 and right as n+1. so for all pivots inbetween he is multiplying with a[left] and a[right] only. but that is not correct

hi… pls share the code u and then tell where what doubt are u facing

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.