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:
function 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 JavaScript
Time spent on task 11 minutes
Notes
not defined yet
Code: 10:46:30 UTC,
java,
autosave
Code: 10:47:35 UTC,
js,
autosave
Code: 10:48:05 UTC,
js,
autosave
Code: 10:48:29 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = 0;
for (let val in A) {
maxEnd = Math.max(0, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return max
}
Code: 10:48:41 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = 0;
for (let val in A) {
maxEnd = Math.max(0, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice
}
Code: 10:49:04 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = 0;
for (let val in A) {
maxEnd = Math.max(0, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Analysis
expand all
Example tests
1.
0.128 s
WRONG ANSWER,
got 1234 expected 5
Code: 10:49:42 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = 0;
for (let val of A) {
maxEnd = Math.max(0, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:49:43 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = 0;
for (let val of A) {
maxEnd = Math.max(0, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Analysis
Code: 10:49:53 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = 0;
for (let val of A) {
maxEnd = Math.max(0, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:50:21 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = 0;
for (let val of A) {
maxEnd = Math.max(0, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:50:51 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = 0;
for (let val of A) {
maxEnd = Math.max(0, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:50:53 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = 0;
for (let val of A) {
maxEnd = Math.max(0, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
User test case 1:
[1, 1, 1, 1]
User test case 2:
[1]
User test case 3:
[-1000000]
User test case 4:
[1000000, 1000000, 1000000, 1000000, 1000000]
User test case 5:
[1, 2, 3, 4, 5, -7]
User test case 6:
[1, 2, -7, 3, 1]
Analysis
expand all
User tests
1.
0.072 s
OK
function result: 4
function result: 4
1.
0.080 s
OK
function result: 1
function result: 1
1.
0.072 s
OK
function result: 0
function result: 0
1.
0.068 s
OK
function result: 5000000
function result: 5000000
1.
0.068 s
OK
function result: 15
function result: 15
1.
0.068 s
OK
function result: 4
function result: 4
Code: 10:53:39 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = ;
for (let val of A) {
maxEnd = Math.max(0, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:53:55 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(0, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:54:06 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = -1000000;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(0, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:54:16 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = -1000000;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(max, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:54:37 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const inputLimit = -1000000
let maxEnd = 0;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(max, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:54:48 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const inputLimit = -1000000
let maxEnd = 0;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(inputLimit, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:55:03 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const inputLimit = -1000000
let maxEnd = 0;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(inputLimit, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
User test case 1:
[1, 1, 1, 1]
User test case 2:
[1]
User test case 3:
[-1000000]
User test case 4:
[1000000, 1000000, 1000000, 1000000, 1000000]
User test case 5:
[1, 2, 3, 4, 5, -7]
User test case 6:
[1, 2, -7, 3, 1]
Analysis
expand all
User tests
1.
0.068 s
OK
function result: 4
function result: 4
1.
0.068 s
OK
function result: 1
function result: 1
1.
0.068 s
OK
function result: -1000000
function result: -1000000
1.
0.068 s
OK
function result: 5000000
function result: 5000000
1.
0.068 s
OK
function result: 15
function result: 15
1.
0.072 s
OK
function result: 3
function result: 3
Code: 10:56:07 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const inputLimit = -1000000
let maxEnd = 0;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(inputLimit, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:56:11 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const inputLimit = -1000000
let maxEnd = 0;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(inputLimit, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
User test case 1:
[1, 1, 1, 1]
User test case 2:
[1]
User test case 3:
[-1000000]
User test case 4:
[-1000000, 1]
User test case 5:
[1000000, 1000000, 1000000, 1000000, 1000000]
User test case 6:
[1, 2, 3, 4, 5, -7]
User test case 7:
[1, 2, -7, 3, 1]
Analysis
expand all
User tests
1.
0.072 s
OK
function result: 4
function result: 4
1.
0.072 s
OK
function result: 1
function result: 1
1.
0.068 s
OK
function result: -1000000
function result: -1000000
1.
0.068 s
OK
function result: -999999
function result: -999999
1.
0.068 s
OK
function result: 5000000
function result: 5000000
1.
0.072 s
OK
function result: 15
function result: 15
1.
0.072 s
OK
function result: 3
function result: 3
Code: 10:56:32 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const inputLimit = -1000000
let maxEnd = 0;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(val, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:56:34 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const inputLimit = -1000000
let maxEnd = 0;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(val, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
User test case 1:
[1, 1, 1, 1]
User test case 2:
[1]
User test case 3:
[-1000000]
User test case 4:
[-1000000, 1]
User test case 5:
[1000000, 1000000, 1000000, 1000000, 1000000]
User test case 6:
[1, 2, 3, 4, 5, -7]
User test case 7:
[1, 2, -7, 3, 1]
Analysis
expand all
User tests
1.
0.132 s
OK
function result: 4
function result: 4
1.
0.132 s
OK
function result: 1
function result: 1
1.
0.132 s
OK
function result: -1000000
function result: -1000000
1.
0.132 s
OK
function result: 1
function result: 1
1.
0.132 s
OK
function result: 5000000
function result: 5000000
1.
0.132 s
OK
function result: 15
function result: 15
1.
0.132 s
OK
function result: 4
function result: 4
Code: 10:56:49 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(val, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Code: 10:57:25 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(val, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
User test case 1:
[1, 1, 1, 1]
User test case 2:
[1]
User test case 3:
[-1000000]
User test case 4:
[-1000000, 1]
User test case 5:
[1000000, 1000000, 1000000, 1000000, 1000000]
User test case 6:
[1, 2, 3, 4, 5, -7]
User test case 7:
[1, 2, -7, 3, 1]
Analysis
expand all
User tests
1.
0.072 s
OK
function result: 4
function result: 4
1.
0.072 s
OK
function result: 1
function result: 1
1.
0.072 s
OK
function result: -1000000
function result: -1000000
1.
0.068 s
OK
function result: 1
function result: 1
1.
0.072 s
OK
function result: 5000000
function result: 5000000
1.
0.072 s
OK
function result: 15
function result: 15
1.
0.072 s
OK
function result: 4
function result: 4
Code: 10:57:28 UTC,
js,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let maxEnd = 0;
let maxSlice = -2147483648;
for (let val of A) {
maxEnd = Math.max(val, maxEnd + val);
maxSlice = Math.max(maxSlice, maxEnd);
}
return maxSlice;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.076 s
OK
2.
0.120 s
OK
3.
0.068 s
OK
1.
0.120 s
OK
2.
0.072 s
OK
3.
0.068 s
OK
4.
0.092 s
OK
5.
0.104 s
OK
6.
0.068 s
OK
7.
0.120 s
OK
8.
0.080 s
OK
9.
0.068 s
OK
1.
0.128 s
OK
2.
0.076 s
OK
3.
0.068 s
OK
4.
0.128 s
OK
5.
0.068 s
OK
6.
0.108 s
OK
7.
0.068 s
OK
8.
0.124 s
OK
9.
0.068 s
OK
10.
0.116 s
OK
11.
0.068 s
OK
12.
0.068 s
OK
13.
0.116 s
OK
14.
0.068 s
OK
15.
0.080 s
OK
16.
0.092 s
OK
17.
0.068 s
OK
18.
0.068 s
OK
19.
0.128 s
OK
20.
0.068 s
OK
21.
0.080 s
OK
22.
0.076 s
OK
23.
0.108 s
OK
24.
0.100 s
OK
25.
0.068 s
OK
26.
0.068 s
OK
27.
0.104 s
OK
1.
0.068 s
OK
1.
0.128 s
OK
1.
0.100 s
OK
1.
0.084 s
OK
1.
0.068 s
OK