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 9 minutes
Notes
not defined yet
Code: 17:38:29 UTC,
java,
autosave
Code: 17:38:42 UTC,
py,
autosave
Code: 17:38:59 UTC,
py,
autosave
Code: 17:39:20 UTC,
py,
autosave
Code: 17:39:35 UTC,
py,
autosave
Code: 17:39:45 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_ending = max_slice = A[0]
print(A[0])
for a in A[1:]:
print(a)
max_ending = max_ending + a
max_slice = max(max_slice, max_ending)
return max_slice
User test case 1:
[-2, 1]
User test case 2:
[-10]
Analysis
expand all
User tests
1.
0.036 s
OK
function result: -1
function result: -1
stdout:
-2 1
1.
0.036 s
OK
function result: -10
function result: -10
stdout:
-10
Code: 17:40:50 UTC,
py,
autosave
Code: 17:41:11 UTC,
py,
autosave
Code: 17:42:16 UTC,
py,
autosave
Code: 17:42:34 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_ending = max_slice = A[0]
print(A[0])
for a in A[1:]:
print(a)
max_ending = max(0,max_ending + a)
max_slice = max(max_slice, max_ending)
return max_slice
User test case 1:
[-2, 1]
User test case 2:
[-10]
Analysis
expand all
User tests
1.
0.048 s
OK
function result: 0
function result: 0
stdout:
-2 1
1.
0.036 s
OK
function result: -10
function result: -10
stdout:
-10
Code: 17:44:39 UTC,
py,
autosave
Code: 17:45:12 UTC,
py,
autosave
Code: 17:45:18 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_ending = max_slice = 0
print(A[0])
for a in A[1:]:
print(a)
max_ending = max(a,max_ending + a)
max_slice = max(max_slice, max_ending)
return max_slice
User test case 1:
[-2, 1]
User test case 2:
[-10]
Analysis
expand all
Example tests
1.
0.036 s
WRONG ANSWER,
got 4 expected 5
stdout:
3 2 -6 4 0
expand all
User tests
1.
0.036 s
OK
function result: 1
function result: 1
stdout:
-2 1
1.
0.036 s
OK
function result: 0
function result: 0
stdout:
-10
Code: 17:46:09 UTC,
py,
autosave
Code: 17:46:11 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_ending = max_slice = 0
print(A[0])
for a in A:
print(a)
max_ending = max(a,max_ending + a)
max_slice = max(max_slice, max_ending)
return max_slice
User test case 1:
[-2, 1]
User test case 2:
[-10]
Analysis
expand all
User tests
1.
0.048 s
OK
function result: 1
function result: 1
stdout:
-2 -2 1
1.
0.056 s
OK
function result: 0
function result: 0
stdout:
-10 -10
Code: 17:46:41 UTC,
py,
autosave
Code: 17:46:48 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_ending = max_slice = A[0]
print(A[0])
for a in A[1:]:
print(a)
max_ending = max(a,max_ending + a)
max_slice = max(max_slice, max_ending)
return max_slice
User test case 1:
[-2, 1]
User test case 2:
[-10]
Analysis
expand all
User tests
1.
0.036 s
OK
function result: 1
function result: 1
stdout:
-2 1
1.
0.036 s
OK
function result: -10
function result: -10
stdout:
-10
Code: 17:47:02 UTC,
py,
autosave
Code: 17:47:06 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_ending = max_slice = A[0]
for a in A[1:]:
max_ending = max(a,max_ending + a)
max_slice = max(max_slice, max_ending)
return max_slice
User test case 1:
[-2, 1]
User test case 2:
[-10]
Analysis
Code: 17:47:16 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
max_ending = max_slice = A[0]
for a in A[1:]:
max_ending = max(a,max_ending + a)
max_slice = max(max_slice, max_ending)
return max_slice
User test case 1:
[-2, 1]
User test case 2:
[-10]
Analysis
Code: 17:47:18 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