what is the difference between recurrences
dp[i] = max(dp[i-1] + a[i],0)
dp[i] = max(dp[i-1] + a[i],a[i])
ans which one is used most
Maximum subarray sum recurrence
0 wala may be used when you have -ve numbers.
even dp[i] = max(dp[i-1] + a[i],a[i]) works for negative numbers
what if a[i] = -10 , dp[i-1] is -1.
Then 0 wala use hoga
if both are positive then the other case.
if a[i] = -10 ,dp[i-1] = -1,
then recurrence dp[i] = max(dp[i-1] + a[i],a[i]) gives -10
recurrence d[i] = max(dp[i-1]+a[i],0) gives 0
then first recurrence gives correct answer
Right.
Thats how both are used.
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.