Maximum sub array sum hackwithinfy solution

Maximum sub array sum hackwithinfy solution

Maximum sub array sum hackwithinfy solution

Solution: In Python 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import sys
def maxSubArraySum(a,size): 
    max_so_far = -sys.maxsize - 1
    max_ending_here = 0
    maxi = 0
    for i in range(0, size):
        max_ending_here = max_ending_here + a[i]
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here
            maxi = max(maxi, L[i])
        if max_ending_here < 0:
            max_ending_here = 0
    return max_so_far - maxi
n = int(input())
L = []
for i in range(n):
    L.append(int(input()))
print(maxSubArraySum(L, n))

Be the first to comment

Leave a Reply

Your email address will not be published.


*