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:
object Solution { def solution(a: Array[Int]): Int }
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 Scala
Time spent on task 6 minutes
Notes
not defined yet
Task timeline
Code: 03:27:43 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Array[Int]): Int = {
scala.util.Sorting.quickSort(A)
val N = A.length
val max3 = A(N - 3) * A(N - 2) * A(N - 1)
// Two large negative numbers
val specialCase = A(0) * A(1) * A(N - 1)
max3 max specialCase
}
}
Analysis
Code: 03:28:26 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Array[Int]): Int = {
scala.util.Sorting.quickSort(A)
val N = A.length
val max3 = A(N - 3) * A(N - 2) * A(N - 1)
// Two large negative numbers
val specialCase = A(0) * A(1) * A(N - 1)
max3 max specialCase
}
}
User test case 1:
[-100, 5, 1, 2]
User test case 2:
[-100, -99, 1, 2, 3]
Analysis
Code: 03:29:03 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Array[Int]): Int = {
scala.util.Sorting.quickSort(A)
val N = A.length
val max3 = A(N - 3) * A(N - 2) * A(N - 1)
// Two large negative numbers
val specialCase = A(0) * A(1) * A(N - 1)
max3 max specialCase
}
}
User test case 1:
[-100, 3, 5, 1, 2]
User test case 2:
[-100, -99, 1, 2, 3]
Analysis
Code: 03:32:28 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Array[Int]): Int = {
scala.util.Sorting.quickSort(A)
val N = A.length
val max3 = A(N - 3) * A(N - 2) * A(N - 1)
// Two large negative numbers
val specialCase = A(0) * A(1) * A(N - 1)
max3 max specialCase
}
}
User test case 1:
[-100, 3, 5, 1, 2]
User test case 2:
[-100, -99, 1, 2, 3]
Analysis
Code: 03:32:51 UTC,
scala,
final,
score: 
100
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Array[Int]): Int = {
scala.util.Sorting.quickSort(A)
val N = A.length
val max3 = A(N - 3) * A(N - 2) * A(N - 1)
// Two large negative numbers
val specialCase = A(0) * A(1) * A(N - 1)
max3 max specialCase
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N * log(N))
expand all
Correctness tests
1.
2.286 s
OK
2.
2.275 s
OK
3.
2.259 s
OK
1.
2.278 s
OK
2.
2.269 s
OK
3.
2.288 s
OK
4.
2.110 s
OK
1.
2.276 s
OK
2.
2.266 s
OK
3.
2.280 s
OK
1.
2.268 s
OK
expand all
Performance tests
1.
2.363 s
OK
1.
2.608 s
OK
1.
2.849 s
OK
1.
2.627 s
OK
1.
2.697 s
OK
2.
2.728 s
OK