Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python-O(N)-Maximum-subarray-value #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'maxSubarray' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts INTEGER_ARRAY arr as parameter.
#

def maxSubarray(arr):
# Write your code here
s= mx = -float('inf') #for the maximum sum

mx_s = 0 #for the maximum sequence

for i in arr:
s = max(i,s+i)
mx= max(mx,s)
mx_s += i if i > 0 else 0
if mx_s == 0:
mx_s = max(arr)
return mx,mx_s


if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

t = int(input().strip())

for t_itr in range(t):
n = int(input().strip())

arr = list(map(int, input().rstrip().split()))

result = maxSubarray(arr)

fptr.write(' '.join(map(str, result)))
fptr.write('\n')

fptr.close()