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:
function solution(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–2024 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
Task timeline
Code: 14:59:50 UTC,
java,
autosave
Code: 14:59:55 UTC,
js,
autosave
function solution(A) {
const sorted = A.sort()
const size = sorted.length
let biggest = 0
biggest = sorted[size-1] * sorted[size-2] * sorted[size-3]
if (sorted[0] < 0 && sorted[1] < 0 && sorted[size-1] > 0) {
const possible = sorted[0] * sorted[1] * sorted[size-1]
if (possible > biggest) {
biggest = possible
}
}
return biggest
}
Code: 15:00:09 UTC,
js,
verify,
result: Passed
function solution(A) {
const sorted = A.sort()
const size = sorted.length
console.log(sorted)
let biggest = 0
biggest = sorted[size-1] * sorted[size-2] * sorted[size-3]
if (sorted[0] < 0 && sorted[1] < 0 && sorted[size-1] > 0) {
const possible = sorted[0] * sorted[1] * sorted[size-1]
if (possible > biggest) {
biggest = possible
}
}
return biggest
}
User test case 1:
[-5, -6, -4, -7, -10]
Analysis
expand all
User tests
1.
0.076 s
OK
function result: -210
function result: -210
stdout:
[ -10, -4, -5, -6, -7 ]
Code: 15:00:29 UTC,
js,
autosave
function solution(A) {
const sorted = A.sort((a, b))
const size = sorted.length
console.log(sorted)
let biggest = 0
biggest = sorted[size-1] * sorted[size-2] * sorted[size-3]
if (sorted[0] < 0 && sorted[1] < 0 && sorted[size-1] > 0) {
const possible = sorted[0] * sorted[1] * sorted[size-1]
if (possible > biggest) {
biggest = possible
}
}
return biggest
}
Code: 15:00:32 UTC,
js,
verify,
result: Passed
function solution(A) {
const sorted = A.sort((a, b) => a - b)
const size = sorted.length
console.log(sorted)
let biggest = 0
biggest = sorted[size-1] * sorted[size-2] * sorted[size-3]
if (sorted[0] < 0 && sorted[1] < 0 && sorted[size-1] > 0) {
const possible = sorted[0] * sorted[1] * sorted[size-1]
if (possible > biggest) {
biggest = possible
}
}
return biggest
}
User test case 1:
[-5, -6, -4, -7, -10]
Analysis
expand all
User tests
1.
0.076 s
OK
function result: -120
function result: -120
stdout:
[ -10, -7, -6, -5, -4 ]
Code: 15:00:44 UTC,
js,
verify,
result: Passed
function solution(A) {
const sorted = A.sort((a, b) => a - b)
const size = sorted.length
// console.log(sorted)
let biggest = 0
biggest = sorted[size-1] * sorted[size-2] * sorted[size-3]
if (sorted[0] < 0 && sorted[1] < 0 && sorted[size-1] > 0) {
const possible = sorted[0] * sorted[1] * sorted[size-1]
if (possible > biggest) {
biggest = possible
}
}
return biggest
}
User test case 1:
[-5, -6, -4, -7, -10]
Analysis
Code: 15:00:49 UTC,
js,
verify,
result: Passed
function solution(A) {
const sorted = A.sort((a, b) => a - b)
const size = sorted.length
// console.log(sorted)
let biggest = 0
biggest = sorted[size-1] * sorted[size-2] * sorted[size-3]
if (sorted[0] < 0 && sorted[1] < 0 && sorted[size-1] > 0) {
const possible = sorted[0] * sorted[1] * sorted[size-1]
if (possible > biggest) {
biggest = possible
}
}
return biggest
}
User test case 1:
[-5, -6, -4, -7, -10]
Analysis
Code: 15:00:52 UTC,
js,
final,
score: 
100
function solution(A) {
const sorted = A.sort((a, b) => a - b)
const size = sorted.length
// console.log(sorted)
let biggest = 0
biggest = sorted[size-1] * sorted[size-2] * sorted[size-3]
if (sorted[0] < 0 && sorted[1] < 0 && sorted[size-1] > 0) {
const possible = sorted[0] * sorted[1] * sorted[size-1]
if (possible > biggest) {
biggest = possible
}
}
return biggest
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N * log(N))
expand all
Correctness tests
1.
0.072 s
OK
2.
0.072 s
OK
3.
0.072 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
3.
0.068 s
OK
4.
0.068 s
OK
1.
0.068 s
OK
2.
0.072 s
OK
3.
0.072 s
OK
1.
0.072 s
OK
expand all
Performance tests
1.
0.084 s
OK
1.
0.112 s
OK
1.
0.192 s
OK
1.
0.128 s
OK
1.
0.120 s
OK
2.
0.116 s
OK