Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 51k
Expand file tree
/
Copy pathmaximum_subsequence.py
More file actions
Latest commit
42 lines (32 loc) · 1.11 KB
/
Copy pathmaximum_subsequence.py
File metadata and controls
42 lines (32 loc) · 1.11 KB
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
37
38
39
40
41
42
fromcollections.abcimportSequence
defmax_subsequence_sum(nums: Sequence[int] |None=None) ->int:
"""Return the maximum possible sum amongst all non - empty subsequences.
Raises:
ValueError: when nums is empty.
>>> max_subsequence_sum([1,2,3,4,-2])
10
>>> max_subsequence_sum([-2, -3, -1, -4, -6])
-1
>>> max_subsequence_sum([])
Traceback (most recent call last):
. . .
ValueError: Input sequence should not be empty
>>> max_subsequence_sum()
Traceback (most recent call last):
. . .
ValueError: Input sequence should not be empty
"""
ifnumsisNoneornotnums:
raiseValueError("Input sequence should not be empty")
ans=nums[0]
foriinrange(1, len(nums)):
num=nums[i]
ans=max(ans, ans+num, num)
returnans
if__name__=="__main__":
importdoctest
doctest.testmod()
# Try on a sample input from the user
n=int(input("Enter number of elements : ").strip())
array=list(map(int, input("\nEnter the numbers : ").strip().split()))[:n]
print(max_subsequence_sum(array))