Tasks Details
easy
Count the number of triangles that 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 it is possible to build a triangle with sides of lengths A[P], A[Q] and A[R]. In other words, 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] = 12There are four triangular triplets that can be constructed from elements of this array, namely (0, 2, 4), (0, 2, 5), (0, 4, 5), and (2, 4, 5).
Write a function:
function solution(A);
that, given an array A consisting of N integers, returns the number of triangular triplets in this array.
For example, given array A such that:
A[0] = 10 A[1] = 2 A[2] = 5 A[3] = 1 A[4] = 8 A[5] = 12the function should return 4, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..1,000];
- each element of array A is an integer within the range [1..1,000,000,000].
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 14 minutes
Notes
not defined yet
Code: 18:09:24 UTC,
java,
autosave
Code: 18:09:24 UTC,
java,
autosave
Code: 18:17:16 UTC,
js,
verify,
result: Failed
Analysis
expand all
Example tests
example
example, positive answer, length=6
example, positive answer, length=6
✘
RUNTIME ERROR
tested program terminated with exit code 1
tested program terminated with exit code 1
1.
0.076 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 1, 2, 5, 8, 10, 12 ]
Code: 18:17:56 UTC,
js,
autosave
Code: 18:18:36 UTC,
js,
autosave
Code: 18:18:49 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
A.sort((a, b) => a - b);
for (let i = 0; i < N; i++) {
let left = i + 1;
let right = i + 2;
while (true) {
if (right < N && right)
}
}
}
Code: 18:19:08 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
A.sort((a, b) => a - b);
for (let i = 0; i < N; i++) {
let left = i + 1;
let right = i + 2;
while (true) {
if (right < N && A[i] + A[left] > right)
}
}
}
Code: 18:19:35 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
A.sort((a, b) => a - b);
for (let i = 0; i < N; i++) {
let left = i + 1;
let right = i + 2;
while (true) {
if (right < N && A[i] + A[left] > A[right]) {
right++;
} else {
}
}
}
}
Code: 18:20:05 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
A.sort((a, b) => a - b);
let count = 0;
for (let i = 0; i < N; i++) {
let left = i + 1;
let right = i + 2;
while (left < N - 1) {
if (right < N && A[i] + A[left] > A[right]) {
right++;
} else {
left++;
count++;
}
}
}
}
Code: 18:20:11 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
A.sort((a, b) => a - b);
let count = 0;
for (let i = 0; i < N; i++) {
let left = i + 1;
let right = i + 2;
while (left < N - 1) {
if (right < N && A[i] + A[left] > A[right]) {
right++;
} else {
left++;
count++;
}
}
}
return count;
}
Analysis
expand all
Example tests
1.
0.072 s
WRONG ANSWER,
got 10 expected 4
Code: 18:20:55 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
A.sort((a, b) => a - b);
let count = 0;
for (let i = 0; i < N; i++) {
let left = i + 1;
let right = i + 2;
while (left < N - 1) {
if (right < N && A[i] + A[left] > A[right]) {
right++;
} else {
left++;
count
}
}
}
return count;
}
Code: 18:21:02 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
A.sort((a, b) => a - b);
let count = 0;
for (let i = 0; i < N; i++) {
let left = i + 1;
let right = i + 2;
while (left < N - 1) {
if (right < N && A[i] + A[left] > A[right]) {
right++;
} else {
left++;
count += right - left;
}
}
}
return count;
}
Analysis
Code: 18:21:27 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
A.sort((a, b) => a - b);
let count = 0;
for (let i = 0; i < N - 2; i++) {
let left = i + 1;
let right = i + 2;
while (left < N - 1) {
if (right < N && A[i] + A[left] > A[right]) {
right++;
} else {
left++;
count += right - left;
}
}
}
return count;
}
Code: 18:21:28 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
A.sort((a, b) => a - b);
let count = 0;
for (let i = 0; i < N - 2; i++) {
let left = i + 1;
let right = i + 2;
while (left < N - 1) {
if (right < N && A[i] + A[left] > A[right]) {
right++;
} else {
left++;
count += right - left;
}
}
}
return count;
}
Analysis
Code: 18:21:49 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
A.sort((a, b) => a - b);
let count = 0;
for (let i = 0; i < N - 2; i++) {
let left = i + 1;
let right = i + 2;
while (left < N - 1) {
if (right < N && A[i] + A[left] > A[right]) {
right++;
} else {
left++;
count += right - left;
}
}
}
return count;
}
Analysis
Code: 18:23:05 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
A.sort((a, b) => a - b);
let count = 0;
for (let i = 0; i < N - 2; i++) {
let left = i + 1;
let right = i + 2;
while (left < N - 1) {
if (right < N && A[i] + A[left] > A[right]) {
right++;
} else {
left++;
count += right - left;
}
}
}
return count;
}
Analysis
Code: 18:23:08 UTC,
js,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
const N = A.length;
A.sort((a, b) => a - b);
let count = 0;
for (let i = 0; i < N - 2; i++) {
let left = i + 1;
let right = i + 2;
while (left < N - 1) {
if (right < N && A[i] + A[left] > A[right]) {
right++;
} else {
left++;
count += right - left;
}
}
}
return count;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N**2)
expand all
Correctness tests
1.
0.072 s
OK
2.
0.072 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
1.
0.072 s
OK
2.
0.072 s
OK
1.
0.072 s
OK
2.
0.068 s
OK
1.
0.068 s
OK
1.
0.072 s
OK
1.
0.072 s
OK