Tasks Details
easy
1.
MaxSliceSum
Find a maximum sum of a compact subsequence of array elements.
Task Score
69%
Correctness
75%
Performance
60%
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–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 8
Time spent on task 54 minutes
Notes
not defined yet
Code: 07:48:37 UTC,
java,
autosave
Code: 08:41:39 UTC,
java,
autosave
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
// 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) {
// write your code in Java SE 8
// 배열의 요소 확인 (모두 음수 or 모두 양수 or 음수/양수 혼합 인지 여부)
boolean isAllMinus = true;
boolean isAllPlus = true;
for (int n : A) {
if (n > 0) {
isAllMinus = false;
}
if (n < 0) {
isAllPlus = false;
}
}
int nMax = Integer.MIN_VALUE;
// 모두 음수인 경우 -> 가장 큰 값 리턴
if (isAllMinus) {
for (int n : A) {
nMax = Math.max(n, nMax);
}
return nMax;
}
// 모두 양수인 경우 -> 모든 값 합하여 리턴
if (isAllPlus) {
return IntStream.of(A).sum();
}
// 음수와 양수가 섞인 경우 -> 음수를 기준으로 slice한 후 각각의 합 중 최대값 리턴
TreeSet<Integer> tSet = new TreeSet<>();
int sliceSum = 0;
for (int i=0; i<A.length; i++) {
if (A[i] >= 0) {
sliceSum += A[i];
} else {
tSet.add(sliceSum);
sliceSum = 0;
}
if (i == A.length-1) {
tSet.add(sliceSum);
}
}
//System.out.println(tSet);
return tSet.last();
}
}
Code: 08:41:49 UTC,
java,
autosave
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
// you can also use imports, for example:
// import java.util.*;
import java.util.TreeSet;
import java.util.stream.IntStream;
// 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) {
// write your code in Java SE 8
// 배열의 요소 확인 (모두 음수 or 모두 양수 or 음수/양수 혼합 인지 여부)
boolean isAllMinus = true;
boolean isAllPlus = true;
for (int n : A) {
if (n > 0) {
isAllMinus = false;
}
if (n < 0) {
isAllPlus = false;
}
}
int nMax = Integer.MIN_VALUE;
// 모두 음수인 경우 -> 가장 큰 값 리턴
if (isAllMinus) {
for (int n : A) {
nMax = Math.max(n, nMax);
}
return nMax;
}
// 모두 양수인 경우 -> 모든 값 합하여 리턴
if (isAllPlus) {
return IntStream.of(A).sum();
}
// 음수와 양수가 섞인 경우 -> 음수를 기준으로 slice한 후 각각의 합 중 최대값 리턴
TreeSet<Integer> tSet = new TreeSet<>();
int sliceSum = 0;
for (int i=0; i<A.length; i++) {
if (A[i] >= 0) {
sliceSum += A[i];
} else {
tSet.add(sliceSum);
sliceSum = 0;
}
if (i == A.length-1) {
tSet.add(sliceSum);
}
}
//System.out.println(tSet);
return tSet.last();
}
}
Code: 08:41:52 UTC,
java,
verify,
result: Passed
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
// you can also use imports, for example:
// import java.util.*;
import java.util.TreeSet;
import java.util.stream.IntStream;
// 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) {
// write your code in Java SE 8
// 배열의 요소 확인 (모두 음수 or 모두 양수 or 음수/양수 혼합 인지 여부)
boolean isAllMinus = true;
boolean isAllPlus = true;
for (int n : A) {
if (n > 0) {
isAllMinus = false;
}
if (n < 0) {
isAllPlus = false;
}
}
int nMax = Integer.MIN_VALUE;
// 모두 음수인 경우 -> 가장 큰 값 리턴
if (isAllMinus) {
for (int n : A) {
nMax = Math.max(n, nMax);
}
return nMax;
}
// 모두 양수인 경우 -> 모든 값 합하여 리턴
if (isAllPlus) {
return IntStream.of(A).sum();
}
// 음수와 양수가 섞인 경우 -> 음수를 기준으로 slice한 후 각각의 합 중 최대값 리턴
TreeSet<Integer> tSet = new TreeSet<>();
int sliceSum = 0;
for (int i=0; i<A.length; i++) {
if (A[i] >= 0) {
sliceSum += A[i];
} else {
tSet.add(sliceSum);
sliceSum = 0;
}
if (i == A.length-1) {
tSet.add(sliceSum);
}
}
//System.out.println(tSet);
return tSet.last();
}
}
Analysis
Code: 08:41:56 UTC,
java,
final,
score: 
69
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
// you can also use imports, for example:
// import java.util.*;
import java.util.TreeSet;
import java.util.stream.IntStream;
// 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) {
// write your code in Java SE 8
// 배열의 요소 확인 (모두 음수 or 모두 양수 or 음수/양수 혼합 인지 여부)
boolean isAllMinus = true;
boolean isAllPlus = true;
for (int n : A) {
if (n > 0) {
isAllMinus = false;
}
if (n < 0) {
isAllPlus = false;
}
}
int nMax = Integer.MIN_VALUE;
// 모두 음수인 경우 -> 가장 큰 값 리턴
if (isAllMinus) {
for (int n : A) {
nMax = Math.max(n, nMax);
}
return nMax;
}
// 모두 양수인 경우 -> 모든 값 합하여 리턴
if (isAllPlus) {
return IntStream.of(A).sum();
}
// 음수와 양수가 섞인 경우 -> 음수를 기준으로 slice한 후 각각의 합 중 최대값 리턴
TreeSet<Integer> tSet = new TreeSet<>();
int sliceSum = 0;
for (int i=0; i<A.length; i++) {
if (A[i] >= 0) {
sliceSum += A[i];
} else {
tSet.add(sliceSum);
sliceSum = 0;
}
if (i == A.length-1) {
tSet.add(sliceSum);
}
}
//System.out.println(tSet);
return tSet.last();
}
}
Analysis summary
The following issues have been detected: wrong answers.
For example, for the input [3, -2, 3] the solution returned a wrong answer (got 3 expected 4).
Analysis
expand all
Correctness tests
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.088 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.008 s
OK
4.
0.008 s
OK
5.
0.088 s
OK
6.
0.084 s
OK
7.
0.004 s
OK
8.
0.084 s
OK
9.
0.088 s
OK
1.
0.004 s
OK
2.
0.008 s
OK
3.
0.008 s
OK
4.
0.008 s
OK
5.
0.008 s
OK
6.
0.008 s
OK
7.
0.004 s
OK
8.
0.008 s
OK
9.
0.004 s
OK
10.
0.008 s
OK
11.
0.004 s
OK
12.
0.008 s
OK
13.
0.008 s
OK
14.
0.084 s
OK
15.
0.088 s
OK
16.
0.004 s
OK
17.
0.092 s
OK
18.
0.088 s
OK
19.
0.008 s
OK
20.
0.008 s
OK
21.
0.004 s
WRONG ANSWER,
got 3 expected 4
22.
0.008 s
OK
23.
0.088 s
OK
24.
0.088 s
OK
25.
0.008 s
OK
26.
0.088 s
OK
27.
0.088 s
OK
1.
0.008 s
OK
1.
0.008 s
OK
1.
0.008 s
WRONG ANSWER,
got 1480 expected 2662
1.
0.008 s
OK
1.
0.088 s
OK
expand all
Performance tests
1.
0.008 s
OK
1.
0.008 s
OK
1.
0.008 s
WRONG ANSWER,
got 91 expected 252
1.
0.456 s
WRONG ANSWER,
got 501 expected 1001
1.
0.536 s
OK