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–2023 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Total time used 2 minutes
Effective time used 2 minutes
Notes
not defined yet
Task timeline
Code: 04:01:17 UTC,
java,
autosave
Code: 04:02:52 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
var sumsFromLeft = A.map(i => 0);
var sumsFromRight = A.map(i => 0);
//a fun opportunity to use a double condition and indexes in a for loop
for (var indexLeft = 1, indexRight = A.length-2; indexRight >= 2; indexLeft++, indexRight--) {
//verify with 0, minimum max sum is 0 anyway
sumsFromLeft[indexLeft] = Math.max(0, sumsFromLeft[indexLeft-1] + A[indexLeft]);
sumsFromRight[indexRight] = Math.max(0, sumsFromRight[indexRight+1] + A[indexRight]);
}
//initialize max with the first double slice sum
var max = sumsFromLeft[0] + sumsFromRight[2];
for (var i = 2; i < A.length-1; i++) {
max = Math.max(max, sumsFromLeft[i-1] + sumsFromRight[i+1]);
}
return max;}
Code: 04:02:57 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) {
var sumsFromLeft = A.map(i => 0);
var sumsFromRight = A.map(i => 0);
//a fun opportunity to use a double condition and indexes in a for loop
for (var indexLeft = 1, indexRight = A.length-2; indexRight >= 2; indexLeft++, indexRight--) {
//verify with 0, minimum max sum is 0 anyway
sumsFromLeft[indexLeft] = Math.max(0, sumsFromLeft[indexLeft-1] + A[indexLeft]);
sumsFromRight[indexRight] = Math.max(0, sumsFromRight[indexRight+1] + A[indexRight]);
}
//initialize max with the first double slice sum
var max = sumsFromLeft[0] + sumsFromRight[2];
for (var i = 2; i < A.length-1; i++) {
max = Math.max(max, sumsFromLeft[i-1] + sumsFromRight[i+1]);
}
return max;
}
Analysis
Code: 04:03:01 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) {
var sumsFromLeft = A.map(i => 0);
var sumsFromRight = A.map(i => 0);
//a fun opportunity to use a double condition and indexes in a for loop
for (var indexLeft = 1, indexRight = A.length-2; indexRight >= 2; indexLeft++, indexRight--) {
//verify with 0, minimum max sum is 0 anyway
sumsFromLeft[indexLeft] = Math.max(0, sumsFromLeft[indexLeft-1] + A[indexLeft]);
sumsFromRight[indexRight] = Math.max(0, sumsFromRight[indexRight+1] + A[indexRight]);
}
//initialize max with the first double slice sum
var max = sumsFromLeft[0] + sumsFromRight[2];
for (var i = 2; i < A.length-1; i++) {
max = Math.max(max, sumsFromLeft[i-1] + sumsFromRight[i+1]);
}
return max;
}
Analysis
Code: 04:03:05 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) {
var sumsFromLeft = A.map(i => 0);
var sumsFromRight = A.map(i => 0);
//a fun opportunity to use a double condition and indexes in a for loop
for (var indexLeft = 1, indexRight = A.length-2; indexRight >= 2; indexLeft++, indexRight--) {
//verify with 0, minimum max sum is 0 anyway
sumsFromLeft[indexLeft] = Math.max(0, sumsFromLeft[indexLeft-1] + A[indexLeft]);
sumsFromRight[indexRight] = Math.max(0, sumsFromRight[indexRight+1] + A[indexRight]);
}
//initialize max with the first double slice sum
var max = sumsFromLeft[0] + sumsFromRight[2];
for (var i = 2; i < A.length-1; i++) {
max = Math.max(max, sumsFromLeft[i-1] + sumsFromRight[i+1]);
}
return max;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.068 s
OK
2.
0.072 s
OK
3.
0.068 s
OK
1.
0.072 s
OK
2.
0.068 s
OK
1.
0.072 s
OK
1.
0.068 s
OK
1.
0.068 s
OK
1.
0.072 s
OK
expand all
Performance tests
1.
0.068 s
OK
1.
0.068 s
OK
1.
0.072 s
OK
1.
0.176 s
OK
1.
0.184 s
OK
1.
0.184 s
OK
1.
0.168 s
OK
2.
0.176 s
OK