DP Approach:

This problem can be solved using Dynamic Programming. Let’s define two terms:

dp[i]dp[i] == the optimal sub-array sum up to index ii by keeping the value of index ii itself

arr[i]arr[ i ] == the value of the array at index ii.

If we take an array index ii and calculate the maximum sub-array sum by keeping the value of index ii, then we have only two options. We can either take only the value at position ii, or we can add the value of position ii with the maximum sub-array sum up to index ii. So, the answer for dp[i]dp[i] would be the maximum of dp[i1]+arr[i]dp[i-1] + arr[i] and arr[i]arr[i]. The solution of the entire problem would be the maximum of all the dp[i]dp[i] from index 00 to N1N-1. The time complexity of the DP solution would be O(N)O(N), where NN is the size of the array.

Statistics

81% Solution Ratio
naeem4265Earliest, Dec '21
Kuddus.6068Fastest, 0.0s
IFTEKHAR_AHAMEDLightest, 131 kB
jahid_hridoyShortest, 280B
Toph uses cookies. By continuing you agree to our Cookie Policy.