0

I need to compute an algorithm that finds the maximal subarray of an array of integers (both positive and negative), and then computes the sum of this subarray. So far I've the following solution, but I reach the code evaluator's time limit when the input becomes large (the input array A can be of up to length 200k).

n = int(input())
A = list(map(int, input().split()))

max_sum = A[0]
current_max = 0
x = 1

for i in range(n):
    for j in range(x,n+1):
        current_max = sum(A[i:j])
        if current_max > max_sum:
            max_sum = current_max 
    x += 1

print(max_sum)

Any help is greatly appreciated.

jsim
  • 11

0 Answers0