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.
# =========================================================
# Subarray sum
sum_arr = 0
max_sum = 0
for i in range(len(arr)):
sum_arr += arr[i]
if sum_arr > max_sum:
max_sum = sum_arr
else:
if sum_arr < 0:
sum_arr = 0
else:
continue
if sum(1 for a in arr if a>0) == 0:
max_sum = max(arr)
#======================================================
#Subsequence sum
if sum(1 for a in arr if a>0) == 0:
subseq_sum = max(arr)
else:
subseq_sum = sum(a for a in arr if a > 0)
return max_sum, subseq_sum
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 →
Python:
def maxSubarray(arr):