We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defmaxSubarray(arr):max_ar,max_se=arr[0],0curr_ar=0forvalinarr:# calculate maximum subarraycurr_ar=max(val,curr_ar+val)max_ar=max(max_ar,curr_ar)# calculate maximum subsequenceifval>0:max_se+=val# if all val in arr are negativeifmax_se==0:max_se=max(arr)returnmax_ar,max_se
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Maximum Subarray
You are viewing a single comment's thread. Return to all comments →