-
Notifications
You must be signed in to change notification settings - Fork 452
/
HackerRank_MaximumSubArray.py
36 lines (31 loc) · 1.22 KB
/
HackerRank_MaximumSubArray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
## Aim:
## Given an array of k elements, find the maximum possible sum of a
## 1. Contiguous subarray
## 2. Non-contiguous (not necessarily contiguous) subarray.
## Input :
## First line of the input has an integer t. t cases follow.
## Each test case begins with an integer k . In the next line, k integers follow representing the elements of array arr.
## Output :
## Two, space separated, integers denoting the maximum contiguous and non-contiguous subarray.
## At least one integer should be selected and put into the subarrays
## (this may be required in cases where all elements are negative).
t = int(input().strip())
def max_subarray(A):
max_ending_here = max_so_far = 0
for x in A:
max_ending_here = max(0, max_ending_here + x)
max_so_far = max(max_so_far, max_ending_here)
return max_so_far
for i in range(2*t):
k=int(input())
arr = [int(arr_temp) for arr_temp in input().strip().split(' ')]
if(all(item>0 for item in arr)):
print(sum(arr),sum(arr))
elif(all(item<0 for item in arr)):
print(max(arr),max(arr))
else:
c=0
for i in range(len(arr)):
if(c+arr[i]>c):
c+=arr[i]
print(max_subarray(arr),c)