Tasks Details
medium
Find the maximal sum of any double slice.
Task Score
100%
Correctness
100%
Performance
100%
A non-empty array A consisting of N integers is given.
A triplet (X, Y, Z), such that 0 ≤ X < Y < Z < N, is called a double slice.
The sum of double slice (X, Y, Z) is the total of A[X + 1] + A[X + 2] + ... + A[Y − 1] + A[Y + 1] + A[Y + 2] + ... + A[Z − 1].
For example, array A such that:
A[0] = 3 A[1] = 2 A[2] = 6 A[3] = -1 A[4] = 4 A[5] = 5 A[6] = -1 A[7] = 2contains the following example double slices:
- double slice (0, 3, 6), sum is 2 + 6 + 4 + 5 = 17,
- double slice (0, 3, 7), sum is 2 + 6 + 4 + 5 − 1 = 16,
- double slice (3, 4, 5), sum is 0.
The goal is to find the maximal sum of any double slice.
Write a function:
function solution(A);
that, given a non-empty array A consisting of N integers, returns the maximal sum of any double slice.
For example, given:
A[0] = 3 A[1] = 2 A[2] = 6 A[3] = -1 A[4] = 4 A[5] = 5 A[6] = -1 A[7] = 2the function should return 17, because no double slice of array A has a sum of greater than 17.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [3..100,000];
- each element of array A is an integer within the range [−10,000..10,000].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 35 minutes
Notes
not defined yet
Code: 16:34:34 UTC,
java,
autosave
Code: 17:03:59 UTC,
js,
autosave
Code: 17:04:09 UTC,
js,
autosave
Code: 17:04:43 UTC,
js,
autosave
Code: 17:04:53 UTC,
js,
autosave
Code: 17:05:24 UTC,
js,
autosave
Code: 17:05:47 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
const left = [0];
const right = [0];
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, )
}
}
Code: 17:05:57 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
const left = [0];
const right = [0];
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, )
}
}
Code: 17:06:14 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
const left = [0];
const right = [0];
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, right[N- ])
}
}
Code: 17:06:38 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
const left = [0];
const right = [0];
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, right[N - i] + A[N - 1 - i]);
}
}
Code: 17:07:09 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
const left = [0];
const right = [0];
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, right[N - i] + A[N - 1 - i]);
}
let maxDoubleSlice = 0;
for (let i = 1; i < N - 1; i++) {
maxDoubleSlice = Math.max(maxDoubleSlice, )
}
}
Code: 17:07:27 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) {
const N = A.length;
const left = [0];
const right = [0];
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, right[N - i] + A[N - 1 - i]);
}
let maxDoubleSlice = 0;
for (let i = 1; i < N - 1; i++) {
maxDoubleSlice = Math.max(maxDoubleSlice, left[i - 1] + right[i + 1]);
}
return maxDoubleSlice;
}
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, non-integer number found
Code: 17:07:42 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
const left = [0];
const right = [0];
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, right[N - i] + A[N - 1 - i]);
}
let maxDoubleSlice = 0;
for (let i = 1; i < N - 1; i++) {
maxDoubleSlice = Math.max(maxDoubleSlice, left[i - 1] + right[i + 1]);
}
return maxDoubleSlice;
}
Code: 17:07:53 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) {
const N = A.length;
const left = [0];
const right = [0];
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, right[N - i] + A[N - 1 - i]);
}
console.log(left);
console.log(right)
let maxDoubleSlice = 0;
for (let i = 1; i < N - 1; i++) {
maxDoubleSlice = Math.max(maxDoubleSlice, left[i - 1] + right[i + 1]);
}
console.log(maxDoubleSlice)
return maxDoubleSlice;
}
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, non-integer number foundstdout:
[ 0, 2, 8, 7, 11, 16, 15 ] [ 0, NaN, NaN, NaN, NaN, NaN, NaN ] NaN
Code: 17:08:33 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
const left = [0];
const right = e;
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, right[N - i] + A[N - 1 - i]);
}
console.log(left);
console.log(right)
let maxDoubleSlice = 0;
for (let i = 1; i < N - 1; i++) {
maxDoubleSlice = Math.max(maxDoubleSlice, left[i - 1] + right[i + 1]);
}
console.log(maxDoubleSlice)
return maxDoubleSlice;
}
Code: 17:08:44 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
const left = [0];
const right = new Array(N).fill(0);
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, right[N - i] + A[N - 1 - i]);
}
console.log(left);
console.log(right)
let maxDoubleSlice = 0;
for (let i = 1; i < N - 1; i++) {
maxDoubleSlice = Math.max(maxDoubleSlice, left[i - 1] + right[i + 1]);
}
console.log(maxDoubleSlice)
return maxDoubleSlice;
}
Code: 17:08:46 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) {
const N = A.length;
const left = new Array(N).fill(0);
const right = new Array(N).fill(0);
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, right[N - i] + A[N - 1 - i]);
}
console.log(left);
console.log(right)
let maxDoubleSlice = 0;
for (let i = 1; i < N - 1; i++) {
maxDoubleSlice = Math.max(maxDoubleSlice, left[i - 1] + right[i + 1]);
}
console.log(maxDoubleSlice)
return maxDoubleSlice;
}
Analysis
expand all
Example tests
1.
0.080 s
OK
stdout:
[ 0, 2, 8, 7, 11, 16, 15, 0 ] [ 0, 16, 14, 8, 9, 5, 0, 0 ] 17
Code: 17:08:54 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) {
const N = A.length;
const left = new Array(N).fill(0);
const right = new Array(N).fill(0);
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, right[N - i] + A[N - 1 - i]);
}
let maxDoubleSlice = 0;
for (let i = 1; i < N - 1; i++) {
maxDoubleSlice = Math.max(maxDoubleSlice, left[i - 1] + right[i + 1]);
}
return maxDoubleSlice;
}
Analysis
Code: 17:08:58 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) {
const N = A.length;
const left = new Array(N).fill(0);
const right = new Array(N).fill(0);
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, right[N - i] + A[N - 1 - i]);
}
let maxDoubleSlice = 0;
for (let i = 1; i < N - 1; i++) {
maxDoubleSlice = Math.max(maxDoubleSlice, left[i - 1] + right[i + 1]);
}
return maxDoubleSlice;
}
Analysis
Code: 17:09:01 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) {
const N = A.length;
const left = new Array(N).fill(0);
const right = new Array(N).fill(0);
for (let i = 1; i < N - 1; i++) {
left[i] = Math.max(0, left[i - 1] + A[i]);
right[N - 1 - i] = Math.max(0, right[N - i] + A[N - 1 - i]);
}
let maxDoubleSlice = 0;
for (let i = 1; i < N - 1; i++) {
maxDoubleSlice = Math.max(maxDoubleSlice, left[i - 1] + right[i + 1]);
}
return maxDoubleSlice;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.072 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
1.
0.072 s
OK
1.
0.072 s
OK
1.
0.072 s
OK
1.
0.072 s
OK
expand all
Performance tests
1.
0.072 s
OK
1.
0.072 s
OK
1.
0.072 s
OK
1.
0.116 s
OK
1.
0.124 s
OK
1.
0.124 s
OK
1.
0.116 s
OK
2.
0.120 s
OK