Tasks Details
easy
1.
Triangle
Determine whether a triangle can be built from a given set of edges.
Task Score
100%
Correctness
100%
Performance
100%
An array A consisting of N integers is given. A triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and:
- A[P] + A[Q] > A[R],
- A[Q] + A[R] > A[P],
- A[R] + A[P] > A[Q].
For example, consider array A such that:
A[0] = 10 A[1] = 2 A[2] = 5 A[3] = 1 A[4] = 8 A[5] = 20Triplet (0, 2, 4) is triangular.
Write a function:
object Solution { def solution(a: Array[Int]): Int }
that, given an array A consisting of N integers, returns 1 if there exists a triangular triplet for this array and returns 0 otherwise.
For example, given array A such that:
A[0] = 10 A[1] = 2 A[2] = 5 A[3] = 1 A[4] = 8 A[5] = 20the function should return 1, as explained above. Given array A such that:
A[0] = 10 A[1] = 50 A[2] = 5 A[3] = 1the function should return 0.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- each element of array A is 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 Scala
Time spent on task 2 minutes
Notes
not defined yet
Task timeline
Code: 20:49:00 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)
@scala.annotation.tailrec
def loop(idx: Int): Int = {
if (idx >= A.length - 2) 0
else if (A(idx) <= 0) loop(idx + 1)
else {
val currentTrio = (A(idx) + A(idx + 1)) - A(idx + 2)
if (currentTrio > 0) 1
else loop(idx + 1)
}
}
loop(0)
}
}
Analysis
Code: 20:49:19 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)
@scala.annotation.tailrec
def loop(idx: Int): Int = {
if (idx >= A.length - 2) 0
else if (A(idx) <= 0) loop(idx + 1)
else {
val currentTrio = (A(idx) + A(idx + 1)) - A(idx + 2)
if (currentTrio > 0) 1
else loop(idx + 1)
}
}
loop(0)
}
}
Analysis
Code: 20:49:34 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)
@scala.annotation.tailrec
def loop(idx: Int): Int = {
if (idx >= A.length - 2) 0
else if (A(idx) <= 0) loop(idx + 1)
else {
val currentTrio = (A(idx) + A(idx + 1)) - A(idx + 2)
if (currentTrio > 0) 1
else loop(idx + 1)
}
}
loop(0)
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N*log(N))
expand all
Correctness tests
1.
2.514 s
OK
2.
2.531 s
OK
3.
2.545 s
OK
4.
2.555 s
OK
5.
2.482 s
OK
1.
2.426 s
OK
2.
2.550 s
OK
3.
2.451 s
OK
4.
2.618 s
OK
5.
2.468 s
OK
1.
2.519 s
OK
2.
2.487 s
OK
3.
2.419 s
OK
4.
2.434 s
OK
5.
2.401 s
OK
1.
2.413 s
OK
2.
2.433 s
OK
3.
2.466 s
OK
4.
2.510 s
OK
5.
2.669 s
OK
1.
2.628 s
OK
2.
2.539 s
OK
3.
2.680 s
OK
4.
2.470 s
OK
5.
2.638 s
OK
1.
2.566 s
OK
2.
2.745 s
OK
3.
2.852 s
OK
4.
2.509 s
OK
5.
2.633 s
OK
1.
2.531 s
OK
2.
2.512 s
OK
3.
2.444 s
OK
4.
2.502 s
OK
5.
2.635 s
OK
1.
2.525 s
OK
2.
2.520 s
OK
3.
2.574 s
OK
4.
2.565 s
OK
5.
2.658 s
OK
1.
2.524 s
OK
2.
2.455 s
OK
3.
2.525 s
OK
4.
2.504 s
OK
5.
2.574 s
OK
1.
2.815 s
OK
2.
2.677 s
OK
3.
2.863 s
OK
4.
2.762 s
OK
5.
2.652 s
OK
expand all
Performance tests
1.
3.095 s
OK
2.
2.479 s
OK
3.
2.637 s
OK
4.
2.558 s
OK
5.
2.405 s
OK
1.
3.105 s
OK
2.
3.118 s
OK
3.
3.000 s
OK
4.
2.958 s
OK
5.
2.905 s
OK
1.
3.469 s
OK
2.
2.946 s
OK
3.
2.898 s
OK
4.
2.791 s
OK
5.
2.529 s
OK
1.
3.075 s
OK
2.
2.563 s
OK
3.
2.591 s
OK
4.
2.410 s
OK
5.
2.466 s
OK
1.
3.312 s
OK
2.
2.565 s
OK
3.
2.657 s
OK
4.
2.737 s
OK
5.
2.670 s
OK
1.
3.154 s
OK
2.
2.755 s
OK
3.
2.717 s
OK
4.
2.602 s
OK
5.
2.523 s
OK