Tasks Details
easy
1.
MaxSliceSum
Find a maximum sum of a compact subsequence of array elements.
Task Score
100%
Correctness
100%
Performance
100%
A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. The sum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q].
Write a function:
def solution(A)
that, given an array A consisting of N integers, returns the maximum sum of any slice of A.
For example, given array A such that:
A[0] = 3 A[1] = 2 A[2] = -6 A[3] = 4 A[4] = 0the function should return 5 because:
- (3, 4) is a slice of A that has sum 4,
- (2, 2) is a slice of A that has sum −6,
- (0, 1) is a slice of A that has sum 5,
- no other slice of A has sum greater than (0, 1).
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..1,000,000];
- each element of array A is an integer within the range [−1,000,000..1,000,000];
- the result will be an integer within the range [−2,147,483,648..2,147,483,647].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Time spent on task 19 minutes
Notes
not defined yet
Task timeline
Code: 08:57:40 UTC,
java,
autosave
Code: 08:58:21 UTC,
py,
autosave
Code: 08:58:28 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3 2 -6 4 0
Code: 08:59:12 UTC,
py,
autosave
Code: 09:02:26 UTC,
py,
autosave
Code: 09:02:41 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3 2 -6 4 0
Code: 09:03:59 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3 2 -6 4 0
Code: 09:05:07 UTC,
py,
autosave
Code: 09:05:33 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3 2 -6 4 0
Code: 09:06:40 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3 2 -6 4 0
Code: 09:07:19 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3 2 -6 4 0
Code: 09:10:56 UTC,
py,
autosave
Code: 09:11:57 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
3 2 -6 4 0
Code: 09:13:19 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 8, in solution add_num_list.append() TypeError: append() takes exactly one argument (0 given)
Code: 09:14:30 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
[3, 5, -1, 4, 4]
Code: 09:14:51 UTC,
py,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
5
Code: 09:15:02 UTC,
py,
verify,
result: Passed
Analysis
Code: 09:15:10 UTC,
py,
verify,
result: Passed
def solution(A):
add_num = 0
add_num_list = []
for num in A:
add_num += num
add_num_list.append(add_num)
if add_num < 0:
add_num = 0
return max(add_num_list)
User test case 1:
[-1]
Analysis
Code: 09:15:21 UTC,
py,
verify,
result: Passed
def solution(A):
add_num = 0
add_num_list = []
for num in A:
add_num += num
add_num_list.append(add_num)
if add_num < 0:
add_num = 0
return max(add_num_list)
User test case 1:
[-1]
User test case 2:
[-1, -8]
Analysis
Code: 09:15:45 UTC,
py,
verify,
result: Passed
def solution(A):
add_num = 0
add_num_list = []
for num in A:
add_num += num
add_num_list.append(add_num)
if add_num < 0:
add_num = 0
return max(add_num_list)
User test case 1:
[-1]
User test case 2:
[-1, -8]
User test case 3:
[1, -8, 9, -7, 19]
Analysis
expand all
User tests
1.
0.036 s
OK
function result: -1
function result: -1
1.
0.036 s
OK
function result: -1
function result: -1
1.
0.036 s
OK
function result: 21
function result: 21
Code: 09:15:59 UTC,
py,
verify,
result: Passed
def solution(A):
add_num = 0
add_num_list = []
for num in A:
add_num += num
add_num_list.append(add_num)
if add_num < 0:
add_num = 0
return max(add_num_list)
User test case 1:
[-1]
User test case 2:
[-1, -8]
User test case 3:
[1, -8, 9, -7, 19]
Analysis
expand all
User tests
1.
0.036 s
OK
function result: -1
function result: -1
1.
0.036 s
OK
function result: -1
function result: -1
1.
0.036 s
OK
function result: 21
function result: 21
Code: 09:16:02 UTC,
py,
final,
score: 
100
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
7.
0.036 s
OK
8.
0.036 s
OK
9.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
7.
0.036 s
OK
8.
0.036 s
OK
9.
0.036 s
OK
10.
0.036 s
OK
11.
0.036 s
OK
12.
0.036 s
OK
13.
0.036 s
OK
14.
0.036 s
OK
15.
0.036 s
OK
16.
0.036 s
OK
17.
0.036 s
OK
18.
0.036 s
OK
19.
0.036 s
OK
20.
0.036 s
OK
21.
0.036 s
OK
22.
0.036 s
OK
23.
0.036 s
OK
24.
0.036 s
OK
25.
0.036 s
OK
26.
0.036 s
OK
27.
0.036 s
OK
1.
0.036 s
OK
1.
0.036 s
OK
1.
0.036 s
OK
1.
0.036 s
OK
1.
0.036 s
OK