Tasks Details
easy
1.
MaxSliceSum
Find a maximum sum of a compact subsequence of array elements.
Task Score
7%
Correctness
0%
Performance
20%
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:
class Solution { public int solution(int[] 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 Java 8
Total time used 1 minutes
Effective time used 1 minutes
Notes
not defined yet
Task timeline
Code: 07:35:29 UTC,
java,
autosave
Code: 07:36:10 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int maxOfSliceSum = -1;
int sliceSum = 0;
int maxOfArray = 0;
for (int i=1; i<A.length; i++) {
sliceSum = A[i-1] + A[i];
maxOfSliceSum = (maxOfSliceSum > sliceSum) ? maxOfSliceSum : sliceSum;
maxOfArray = (maxOfArray > A[i]) ? maxOfArray : A[i];
}
return (maxOfSliceSum > maxOfArray) ? maxOfSliceSum : maxOfArray;
}
}
Analysis
Code: 07:36:15 UTC,
java,
verify,
result: Passed
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int maxOfSliceSum = -1;
int sliceSum = 0;
int maxOfArray = 0;
for (int i=1; i<A.length; i++) {
sliceSum = A[i-1] + A[i];
maxOfSliceSum = (maxOfSliceSum > sliceSum) ? maxOfSliceSum : sliceSum;
maxOfArray = (maxOfArray > A[i]) ? maxOfArray : A[i];
}
return (maxOfSliceSum > maxOfArray) ? maxOfSliceSum : maxOfArray;
}
}
Analysis
Code: 07:36:20 UTC,
java,
final,
score: 
7
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int maxOfSliceSum = -1;
int sliceSum = 0;
int maxOfArray = 0;
for (int i=1; i<A.length; i++) {
sliceSum = A[i-1] + A[i];
maxOfSliceSum = (maxOfSliceSum > sliceSum) ? maxOfSliceSum : sliceSum;
maxOfArray = (maxOfArray > A[i]) ? maxOfArray : A[i];
}
return (maxOfSliceSum > maxOfArray) ? maxOfSliceSum : maxOfArray;
}
}
Analysis summary
The following issues have been detected: wrong answers.
For example, for the input [10] the solution returned a wrong answer (got 0 expected 10).
Analysis
expand all
Correctness tests
1.
0.004 s
WRONG ANSWER,
got 0 expected -10
2.
0.004 s
OK
3.
0.004 s
WRONG ANSWER,
got 0 expected 10
1.
0.004 s
WRONG ANSWER,
got 0 expected -2
2.
0.004 s
OK
3.
0.008 s
OK
4.
0.004 s
WRONG ANSWER,
got 0 expected 1
5.
0.004 s
OK
6.
0.008 s
OK
7.
0.004 s
WRONG ANSWER,
got 1 expected 3
8.
0.004 s
OK
9.
0.008 s
OK
1.
0.008 s
WRONG ANSWER,
got 0 expected -2
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
5.
0.004 s
OK
6.
0.004 s
OK
7.
0.004 s
OK
8.
0.004 s
OK
9.
0.004 s
OK
10.
0.004 s
WRONG ANSWER,
got 0 expected 1
11.
0.008 s
OK
12.
0.004 s
OK
13.
0.004 s
OK
14.
0.004 s
WRONG ANSWER,
got 2 expected 3
15.
0.004 s
WRONG ANSWER,
got 4 expected 5
16.
0.008 s
OK
17.
0.004 s
WRONG ANSWER,
got 4 expected 5
18.
0.004 s
WRONG ANSWER,
got 6 expected 7
19.
0.004 s
WRONG ANSWER,
got 1 expected 3
20.
0.004 s
WRONG ANSWER,
got 1 expected 3
21.
0.004 s
WRONG ANSWER,
got 3 expected 4
22.
0.004 s
OK
23.
0.004 s
WRONG ANSWER,
got 4 expected 5
24.
0.004 s
WRONG ANSWER,
got 4 expected 7
25.
0.004 s
OK
26.
0.004 s
WRONG ANSWER,
got 6 expected 7
27.
0.004 s
WRONG ANSWER,
got 6 expected 9
1.
0.004 s
WRONG ANSWER,
got 43 expected 53
1.
0.004 s
WRONG ANSWER,
got 0 expected -1000000
1.
0.004 s
WRONG ANSWER,
got 1446 expected 2662
1.
0.004 s
WRONG ANSWER,
got 0 expected -11
1.
0.004 s
WRONG ANSWER,
got 22 expected 550
expand all
Performance tests
1.
0.008 s
WRONG ANSWER,
got 2 expected 1000
1.
0.008 s
OK
1.
0.004 s
WRONG ANSWER,
got 19 expected 252
1.
0.204 s
WRONG ANSWER,
got 501 expected 1001
1.
0.528 s
WRONG ANSWER,
got 0 expected -1