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 2 minutes
Notes
not defined yet
Code: 16:32:03 UTC,
java,
autosave
Code: 16:32:38 UTC,
js,
autosave
function solution(A) {
const N = A.length;
let maxSlice = Number.MIN_SAFE_INTEGER;
let maxEnding = 0;
for (let i = 0; i < N; i++) {
maxEnding = Math.max(maxEnding + A[i], 0);
maxSlice = Math.max(maxSlice, maxEnding);
}
return maxSlice;
}
/*function solution(A) {
const N = A.length;
let maxSlice = A[0]; // maximum sum
let acc = 0; // storing cumulative sum
for (let i = 0; i < N; i++) {
acc += A[i];
maxSlice = Math.max(maxSlice, acc);
// if acc is negative at the current step, it restarts cumulative sum by assigning 0
if (acc < 0) {
acc = 0;
}
}
return maxSlice;
}*/
Code: 16:33:10 UTC,
js,
autosave
function solution(A) {
const N = A.length;
let maxSlice = Number.MIN_SAFE_INTEGER;
let maxEnding = 0;
for (let i = 0; i < N; i++) {
maxEnding = Math.max(maxEnding + A[i], A);
maxSlice = Math.max(maxSlice, maxEnding);
}
return maxSlice;
}
/*function solution(A) {
const N = A.length;
let maxSlice = A[0]; // maximum sum
let acc = 0; // storing cumulative sum
for (let i = 0; i < N; i++) {
acc += A[i];
maxSlice = Math.max(maxSlice, acc);
// if acc is negative at the current step, it restarts cumulative sum by assigning 0
if (acc < 0) {
acc = 0;
}
}
return maxSlice;
}*/
Code: 16:33:13 UTC,
js,
verify,
result: Passed
function solution(A) {
const N = A.length;
let maxSlice = Number.MIN_SAFE_INTEGER;
let maxEnding = 0;
for (let i = 0; i < N; i++) {
maxEnding = Math.max(maxEnding + A[i], A[i]);
maxSlice = Math.max(maxSlice, maxEnding);
}
return maxSlice;
}
/*function solution(A) {
const N = A.length;
let maxSlice = A[0]; // maximum sum
let acc = 0; // storing cumulative sum
for (let i = 0; i < N; i++) {
acc += A[i];
maxSlice = Math.max(maxSlice, acc);
// if acc is negative at the current step, it restarts cumulative sum by assigning 0
if (acc < 0) {
acc = 0;
}
}
return maxSlice;
}*/
Analysis
Code: 16:33:19 UTC,
js,
verify,
result: Passed
function solution(A) {
const N = A.length;
let maxSlice = Number.MIN_SAFE_INTEGER;
let maxEnding = 0;
for (let i = 0; i < N; i++) {
maxEnding = Math.max(maxEnding + A[i], A[i]);
maxSlice = Math.max(maxSlice, maxEnding);
}
return maxSlice;
}
/*function solution(A) {
const N = A.length;
let maxSlice = A[0]; // maximum sum
let acc = 0; // storing cumulative sum
for (let i = 0; i < N; i++) {
acc += A[i];
maxSlice = Math.max(maxSlice, acc);
// if acc is negative at the current step, it restarts cumulative sum by assigning 0
if (acc < 0) {
acc = 0;
}
}
return maxSlice;
}*/
User test case 1:
[-10]
Analysis
Code: 16:33:53 UTC,
js,
verify,
result: Passed
function solution(A) {
const N = A.length;
let maxSlice = Number.MIN_SAFE_INTEGER;
let maxEnding = 0;
for (let i = 0; i < N; i++) {
maxEnding = Math.max(maxEnding + A[i], A[i]);
maxSlice = Math.max(maxSlice, maxEnding);
}
return maxSlice;
}
/*function solution(A) {
const N = A.length;
let maxSlice = A[0]; // maximum sum
let acc = 0; // storing cumulative sum
for (let i = 0; i < N; i++) {
acc += A[i];
maxSlice = Math.max(maxSlice, acc);
// if acc is negative at the current step, it restarts cumulative sum by assigning 0
if (acc < 0) {
acc = 0;
}
}
return maxSlice;
}*/
User test case 1:
[-10]
Analysis
Code: 16:33:55 UTC,
js,
final,
score: 
100
function solution(A) {
const N = A.length;
let maxSlice = Number.MIN_SAFE_INTEGER;
let maxEnding = 0;
for (let i = 0; i < N; i++) {
maxEnding = Math.max(maxEnding + A[i], A[i]);
maxSlice = Math.max(maxSlice, maxEnding);
}
return maxSlice;
}
/*function solution(A) {
const N = A.length;
let maxSlice = A[0]; // maximum sum
let acc = 0; // storing cumulative sum
for (let i = 0; i < N; i++) {
acc += A[i];
maxSlice = Math.max(maxSlice, acc);
// if acc is negative at the current step, it restarts cumulative sum by assigning 0
if (acc < 0) {
acc = 0;
}
}
return maxSlice;
}*/
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.112 s
OK
2.
0.072 s
OK
3.
0.068 s
OK
4.
0.072 s
OK
5.
0.068 s
OK
6.
0.068 s
OK
7.
0.068 s
OK
8.
0.068 s
OK
9.
0.072 s
OK
1.
0.072 s
OK
2.
0.068 s
OK
3.
0.072 s
OK
4.
0.068 s
OK
5.
0.068 s
OK
6.
0.072 s
OK
7.
0.072 s
OK
8.
0.072 s
OK
9.
0.072 s
OK
10.
0.072 s
OK
11.
0.068 s
OK
12.
0.068 s
OK
13.
0.068 s
OK
14.
0.072 s
OK
15.
0.068 s
OK
16.
0.072 s
OK
17.
0.072 s
OK
18.
0.072 s
OK
19.
0.068 s
OK
20.
0.068 s
OK
21.
0.072 s
OK
22.
0.072 s
OK
23.
0.072 s
OK
24.
0.072 s
OK
25.
0.072 s
OK
26.
0.072 s
OK
27.
0.072 s
OK
1.
0.072 s
OK
1.
0.072 s
OK
1.
0.072 s
OK
1.
0.072 s
OK
1.
0.072 s
OK