Tasks Details
easy
Maximize A[P] * A[Q] * A[R] for any triplet (P, Q, R).
Task Score
100%
Correctness
100%
Performance
100%
A non-empty array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N).
For example, array A such that:
A[0] = -3 A[1] = 1 A[2] = 2 A[3] = -2 A[4] = 5 A[5] = 6contains the following example triplets:
- (0, 1, 2), product is −3 * 1 * 2 = −6
- (1, 2, 4), product is 1 * 2 * 5 = 10
- (2, 4, 5), product is 2 * 5 * 6 = 60
Your goal is to find the maximal product of any triplet.
Write a function:
class Solution { public int solution(int[] A); }
that, given a non-empty array A, returns the value of the maximal product of any triplet.
For example, given array A such that:
A[0] = -3 A[1] = 1 A[2] = 2 A[3] = -2 A[4] = 5 A[5] = 6the function should return 60, as the product of triplet (2, 4, 5) is maximal.
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 [−1,000..1,000].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Java 21
Time spent on task 27 minutes
Notes
not defined yet
Code: 13:25:07 UTC,
java,
autosave
Code: 13:25:12 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
Code: 13:25:21 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
Analysis
User test case 1:
[-5, -6, -4, -7, -10]
Code: 13:26:07 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
//
Code: 13:26:37 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
// - - +
// - - -
// - + +
// + + +
Code: 13:27:21 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
// - - +
// - - -
// - + +
// + + +
Code: 13:27:32 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
45 6 710
// - - +
// - - -
// - + +
// + + +
Code: 13:27:45 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
70 20
// - - +
// - - -
// - + +
// + + +
Code: 13:27:55 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - - +
// - - -
// - + +
// + + +
Code: 13:28:18 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
70 20
// - - +
// - - -
// - + +
// + + +
Code: 13:28:29 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - - +
// - - -
// - + +
// + + +
Code: 13:28:58 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - +
// + -
Code: 13:29:33 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - +
// + -
//
Code: 13:29:43 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - +
// + -
// - -
Code: 13:30:03 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - +
// + -
// - - 큰 수
// + + 큰 수
Code: 13:30:13 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - + 작은 수
// + - 작은 수
// - - 큰 수
// + + 큰 수
Code: 13:30:23 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - + 작은 수
// + - 작은 수
// - - 큰 수
// + + 큰 수
//
Code: 13:30:36 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - + 작은 수
// + - 작은 수
// - - 큰 수
// + + 큰 수
// + -
Code: 13:30:48 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - + 작은 수
// + - 작은 수
// - - 큰 수
// + + 큰 수
// + -
Code: 13:31:07 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - + 작은 수
// + - 작은 수
// - - 큰 수
// + + 큰 수
// + - 크게 나올 수 있는 수
//
Code: 13:31:42 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - + 작은 수
// + - 작은 수
// - - 큰 수
// + + 큰 수
// + - 크게 나올 수 있는 수
// ++
Code: 13:31:53 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
10 7 6 5 4
// - + 작은 수
// + - 작은 수
// - - 큰 수
// + + 큰 수
// + - 크게 나올 수 있는 수
//
Code: 13:37:38 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
Code: 13:37:38 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
Analysis
User test case 1:
[-5, -6, -4, -7, -10]
User test case 2:
[-5, -5, 4, 3]
Code: 13:37:57 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
Code: 13:38:38 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 1];
}
return answer;
}
}
Code: 13:39:20 UTC,
java,
verify,
result: Failed
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 1];
}
return answer;
}
}
Analysis
expand all
Example tests
1.
0.004 s
WRONG ANSWER,
got 180 expected 60
User test case 1:
[-5, -6, -4, -7, -10]
User test case 2:
[-5, -5, 4, 3]
Code: 13:39:51 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
뇬
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 1];
}
return answer;
}
}
Code: 13:39:59 UTC,
java,
verify,
result: Failed
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
System.out.println(firstTwoMul);
System.out.println(lastTwoMul);
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 1];
}
return answer;
}
}
Analysis
expand all
Example tests
1.
0.008 s
WRONG ANSWER,
got 180 expected 60
stdout:
6 30
expand all
User tests
1.
0.008 s
OK
function result: -280
function result: -280
stdout:
70 20
1.
0.008 s
OK
function result: 100
function result: 100
stdout:
25 12
User test case 1:
[-5, -6, -4, -7, -10]
User test case 2:
[-5, -5, 4, 3]
Code: 13:40:42 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
System.out.println(firstTwoMul);
System.out.println(lastTwoMul);
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
Code: 13:40:54 UTC,
java,
verify,
result: Passed
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int firstTwoMul = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
System.out.println(firstTwoMul);
System.out.println(lastTwoMul);
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
Analysis
expand all
User tests
1.
0.008 s
OK
function result: -280
function result: -280
stdout:
70 20
1.
0.004 s
OK
function result: 100
function result: 100
stdout:
25 12
User test case 1:
[-5, -6, -4, -7, -10]
User test case 2:
[-5, -5, 4, 3]
Code: 13:49:51 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
int answer;
Arrays.sort(A);
int answer = A[0] * A[1];
int lastTwoMul = A[A.length - 1] * A[A.length - 2];
System.out.println(firstTwoMul);
System.out.println(lastTwoMul);
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
Code: 13:50:15 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
Arrays.sort(A);
int answer1 = A[0] * A[1] * A[A.length - 1];
int answer2 = A[0] * A[1] * A[A.length - 1];
System.out.println(firstTwoMul);
System.out.println(lastTwoMul);
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
Code: 13:50:26 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
Arrays.sort(A);
int answer1 = A[0] * A[1] * A[A.length - 1];
int answer2 = A[A.length - 1] * A[A.length - 2] * A[A.length - 3];
System.out.println(firstTwoMul);
System.out.println(lastTwoMul);
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
Code: 13:50:40 UTC,
java,
autosave
import java.util.*;
class Solution {
public int solution(int[] A) {
Arrays.sort(A);
int N = A.length;
int answer1 = A[0] * A[1] * A[N - 1];
int answer2 = A[N - 1] * A[A.length - 2] * A[A.length - 3];
System.out.println(firstTwoMul);
System.out.println(lastTwoMul);
if (firstTwoMul > lastTwoMul) {
answer = firstTwoMul * A[A.length - 1];
} else {
answer = lastTwoMul * A[A.length - 3];
}
return answer;
}
}
Code: 13:51:08 UTC,
java,
autosave
Code: 13:51:08 UTC,
java,
verify,
result: Failed
Analysis
Compile error
Solution.java:10: error: ';' expected return answer1 > answer2 : answer1 : answer2; ^ Solution.java:10: error: not a statement return answer1 > answer2 : answer1 : answer2; ^ 2 errors
Code: 13:51:19 UTC,
java,
verify,
result: Passed
Analysis
User test case 1:
[-5, -6, -4, -7, -10]
User test case 2:
[-5, -5, 4, 3]
Code: 13:51:46 UTC,
java,
verify,
result: Passed
Analysis
User test case 1:
[-5, -6, -4, -7, -10]
User test case 2:
[-5, -5, 4, 3]
Code: 13:51:50 UTC,
java,
final,
score: 
100
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N * log(N))
expand all
Correctness tests
1.
0.008 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
4.
0.004 s
OK
1.
0.004 s
OK
2.
0.004 s
OK
3.
0.004 s
OK
1.
0.004 s
OK
expand all
Performance tests
1.
0.008 s
OK
1.
0.028 s
OK
1.
0.228 s
OK
1.
0.080 s
OK
1.
0.156 s
OK
2.
0.228 s
OK