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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Total time used 12 minutes
Effective time used 12 minutes
Notes
not defined yet
Task timeline
Code: 13:53:57 UTC,
java,
autosave
Code: 13:54:27 UTC,
py,
autosave
Code: 13:54:57 UTC,
py,
autosave
Code: 13:55:09 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxEnding = -1 * 2**31
maxSlice = -1 * 2**31
print(maxEnding)
for i in range(len(A)):
maxEnding = max(0, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 13:55:10 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxEnding = -1 * 2**31
maxSlice = -1 * 2**31
print(maxEnding)
for i in range(len(A)):
maxEnding = max(0, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Analysis
expand all
Example tests
1.
0.036 s
WRONG ANSWER,
got 4 expected 5
stdout:
-2147483648
Code: 13:55:23 UTC,
py,
autosave
Code: 13:55:51 UTC,
py,
autosave
Code: 13:56:03 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
smallest = -1 * 2**31
maxEnding = -1 * 2**31
maxSlice = -1 * 2**31
for i in range(len(A)):
maxEnding = max(0, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 13:56:17 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
smallest = -1 * 2**31
maxEnding = smallest
maxSlice = smallest
for i in range(len(A)):
maxEnding = max(0, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 13:56:33 UTC,
py,
autosave
Code: 13:56:45 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
smallest = -1 * 2**31
maxEnding = 0
maxSlice = smallest
for i in range(len(A)):
maxEnding = max(smallest, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 13:56:49 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
smallest = -1 * 2**31
maxEnding = 0
maxSlice = smallest
for i in range(len(A)):
maxEnding = max(smallest, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Analysis
Code: 13:56:57 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
smallest = -1 * 2**31
maxEnding = 0
maxSlice = smallest
for i in range(len(A)):
maxEnding = max(smallest, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
User test case 1:
[-10]
Analysis
Code: 13:57:20 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
smallest = -1 * 2**31
maxEnding = 0
maxSlice = smallest
for i in range(len(A)):
maxEnding = max(smallest, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
User test case 1:
[-10]
User test case 2:
[-10, -100, -1]
Analysis
Code: 13:57:40 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
smallest = -1 * 2**31
maxEnding = 0
maxSlice = smallest
for i in range(len(A)):
maxEnding = max(smallest, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 13:57:54 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
smallest = -1 * 2**31
maxEnding = 0
maxSlice = smallest
for i in range(len(A)):
maxEnding = max(smallest, maxEnding+A[i])
print(maxEnding)
maxSlice = max(maxEnding, maxSlice)
return maxSlice
User test case 1:
[-10]
User test case 2:
[-10, -100, -1]
Analysis
expand all
User tests
1.
0.036 s
OK
function result: -10
function result: -10
stdout:
-10
1.
0.036 s
OK
function result: -10
function result: -10
stdout:
-10 -110 -111
Code: 13:58:10 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
smallest = -1 * 2**31
maxEnding = 0
maxSlice = smallest
for i in range(len(A)):
maxEnding = max(smallest, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 13:58:20 UTC,
py,
autosave
Code: 14:00:54 UTC,
py,
autosave
Code: 14:00:59 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
smallest = -1 * 2**31
maxEnding = 0
maxSlice = smallest
for i in range(len(A)):
maxEnding = max(maxEnding, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
User test case 1:
[-10]
User test case 2:
[-10, -100, -1]
Analysis
expand all
Example tests
1.
0.052 s
WRONG ANSWER,
got 9 expected 5
Code: 14:01:24 UTC,
py,
autosave
Code: 14:01:34 UTC,
py,
autosave
Code: 14:02:04 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxEnding = 0
maxSlice = 0
negative
for i in range(len(A)):
for i in range(len(A)):
maxEnding = max(maxEnding, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 14:02:27 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxEnding = 0
maxSlice = 0
allNegative = 0
for i in range(len(A)):
if A[i] >= 0
for i in range(len(A)):
maxEnding = max(maxEnding, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 14:02:52 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxEnding = 0
maxSlice = 0
allNegative = 0
minNegative =
for i in range(len(A)):
if A[i] > 0
for i in range(len(A)):
maxEnding = max(maxEnding, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 14:03:10 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxEnding = 0
maxSlice = 0
allNegative = 0
minNegative = 0
for i in range(len(A)):
if A[i] > 0:
allNegative =
elif
for i in range(len(A)):
maxEnding = max(maxEnding, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 14:03:21 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxEnding = 0
maxSlice = 0
allNegative = 1
minNegative = 0
for i in range(len(A)):
if A[i] > 0:
allNegative = 0
break
elif
for i in range(len(A)):
maxEnding = max(maxEnding, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 14:03:31 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxEnding = 0
maxSlice = 0
allNegative = 1
minNegative = 0
for i in range(len(A)):
if A[i] > 0:
allNegative = 0
break
elif
for i in range(len(A)):
maxEnding = max(maxEnding, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 14:03:42 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxEnding = 0
maxSlice = 0
allNegative = 1
minNegative = 0
for i in range(len(A)):
if A[i] > 0:
allNegative = 0
break
elif A[i] < minNegative
for i in range(len(A)):
maxEnding = max(maxEnding, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 14:04:11 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxEnding = 0
maxSlice = 0
allNegative = 1
maxNegative = -1 * 2**31
for i in range(len(A)):
if A[i] > 0:
allNegative = 0
break
elif A[i] > maxNegative
maxNegative = A[i]
for i in range(len(A)):
maxEnding = max(maxEnding, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 14:04:42 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxEnding = 0
maxSlice = 0
allNegative = 1
maxNegative = -1 * 2**31
for i in range(len(A)):
if A[i] > 0:
allNegtive = 0
break
elif A[i] > maxNegtive
maxNegtive = A[i]
if allNegtive == 1
return maxNegtive
for i in range(len(A)):
maxEnding = max(maxEnding, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 14:04:55 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
maxEnding = 0
maxSlice = 0
allNegtive = 1
maxNegtive = -1 * 2**31
for i in range(len(A)):
if A[i] > 0:
allNegtive = 0
break
elif A[i] > maxNegtive:
maxNegtive = A[i]
if allNegtive == 1
return maxNegtive
for i in range(len(A)):
maxEnding = max(maxEnding, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 14:05:13 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
allNegtive = 1
maxNegtive = -1 * 2**31
for i in range(len(A)):
if A[i] > 0:
allNegtive = 0
break
elif A[i] > maxNegtive:
maxNegtive = A[i]
if allNegtive == 1:
return maxNegtive
maxEnding = 0
maxSlice = 0
for i in range(len(A)):
maxEnding = max(maxEnding, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
Code: 14:05:21 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
allNegtive = 1
maxNegtive = -1 * 2**31
for i in range(len(A)):
if A[i] > 0:
allNegtive = 0
break
elif A[i] > maxNegtive:
maxNegtive = A[i]
if allNegtive == 1:
return maxNegtive
maxEnding = 0
maxSlice = 0
for i in range(len(A)):
maxEnding = max(0, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
User test case 1:
[-10]
User test case 2:
[-10, -100, -1]
Analysis
Code: 14:05:30 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
allNegtive = 1
maxNegtive = -1 * 2**31
for i in range(len(A)):
if A[i] > 0:
allNegtive = 0
break
elif A[i] > maxNegtive:
maxNegtive = A[i]
if allNegtive == 1:
return maxNegtive
maxEnding = 0
maxSlice = 0
for i in range(len(A)):
maxEnding = max(0, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
User test case 1:
[-10]
User test case 2:
[-10, -100, -1]
Analysis
Code: 14:05:32 UTC,
py,
final,
score: 
100
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
allNegtive = 1
maxNegtive = -1 * 2**31
for i in range(len(A)):
if A[i] > 0:
allNegtive = 0
break
elif A[i] > maxNegtive:
maxNegtive = A[i]
if allNegtive == 1:
return maxNegtive
maxEnding = 0
maxSlice = 0
for i in range(len(A)):
maxEnding = max(0, maxEnding+A[i])
maxSlice = max(maxEnding, maxSlice)
return maxSlice
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