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 17 minutes
Notes
not defined yet
Code: 04:09:29 UTC,
java,
autosave
Code: 04:09:33 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) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
let cnt = 0;
for(let i = A.length-1; i>1; i--){
let tail = i-1;
let head = i-2;
while(A[head]+ A[tail] > A[i]){
let tailT = tail;
let headT = head;
while(A[headT]+ A[tailT] > A[i]){
cnt++;
headT--;
}
head--;
tail--;
}
}
return cnt;
}
Analysis
Code: 04:10:25 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) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
console.log(A)
let cnt = 0;
for(let i = A.length-1; i>1; i--){
let tail = i-1;
let head = i-2;
while(A[head]+ A[tail] > A[i]){
let tailT = tail;
let headT = head;
while(A[headT]+ A[tailT] > A[i]){
cnt++;
headT--;
}
head--;
tail--;
}
}
return cnt;
}
Analysis
expand all
Example tests
1.
0.076 s
OK
stdout:
[ 1, 2, 5, 8, 10, 12 ]
Code: 04:15:51 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
console.log(A)
let cnt = 0;
for()
for P_idx in xrange(0, len(A)-2):
Q_idx = P_idx + 1
R_idx = P_idx + 2
while (R_idx < len(A)):
if A[P_idx] + A[Q_idx] > A[R_idx]:
triangle_cnt += R_idx - Q_idx
R_idx += 1
elif Q_idx < R_idx -1:
Q_idx += 1
else:
R_idx += 1
Q_idx += 1
for(let i = A.length-1; i>1; i--){
let tail = i-1;
let head = i-2;
while(A[head]+ A[tail] > A[i]){
let tailT = tail;
let headT = head;
while(A[headT]+ A[tailT] > A[i]){
cnt++;
headT--;
}
head--;
tail--;
}
}
return cnt;
}
Code: 04:16:01 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
console.log(A)
let cnt = 0;
for(let i=0;)
for P_idx in xrange(0, len(A)-2):
Q_idx = P_idx + 1
R_idx = P_idx + 2
while (R_idx < len(A)):
if A[P_idx] + A[Q_idx] > A[R_idx]:
triangle_cnt += R_idx - Q_idx
R_idx += 1
elif Q_idx < R_idx -1:
Q_idx += 1
else:
R_idx += 1
Q_idx += 1
for(let i = A.length-1; i>1; i--){
let tail = i-1;
let head = i-2;
while(A[head]+ A[tail] > A[i]){
let tailT = tail;
let headT = head;
while(A[headT]+ A[tailT] > A[i]){
cnt++;
headT--;
}
head--;
tail--;
}
}
return cnt;
}
Code: 04:16:31 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
console.log(A)
let cnt = 0;
for(let i=0; i<A.length-2; i++){
let Q = i+1;
let R = i+2;
while()
}
for P_idx in xrange(0, len(A)-2):
Q_idx = P_idx + 1
R_idx = P_idx + 2
while (R_idx < len(A)):
if A[P_idx] + A[Q_idx] > A[R_idx]:
triangle_cnt += R_idx - Q_idx
R_idx += 1
elif Q_idx < R_idx -1:
Q_idx += 1
else:
R_idx += 1
Q_idx += 1
for(let i = A.length-1; i>1; i--){
let tail = i-1;
let head = i-2;
while(A[head]+ A[tail] > A[i]){
let tailT = tail;
let headT = head;
while(A[headT]+ A[tailT] > A[i]){
cnt++;
headT--;
}
head--;
tail--;
}
}
return cnt;
}
Code: 04:16:41 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
console.log(A)
let cnt = 0;
for(let i=0; i<A.length-2; i++){
let Q = i+1;
let R = i+2;
while(R < A.length){
}
}
for P_idx in xrange(0, len(A)-2):
Q_idx = P_idx + 1
R_idx = P_idx + 2
while (R_idx < len(A)):
if A[P_idx] + A[Q_idx] > A[R_idx]:
triangle_cnt += R_idx - Q_idx
R_idx += 1
elif Q_idx < R_idx -1:
Q_idx += 1
else:
R_idx += 1
Q_idx += 1
for(let i = A.length-1; i>1; i--){
let tail = i-1;
let head = i-2;
while(A[head]+ A[tail] > A[i]){
let tailT = tail;
let headT = head;
while(A[headT]+ A[tailT] > A[i]){
cnt++;
headT--;
}
head--;
tail--;
}
}
return cnt;
}
Code: 04:17:11 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
console.log(A)
let cnt = 0;
for(let i=0; i<A.length-2; i++){
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
}
}
}
for P_idx in xrange(0, len(A)-2):
Q_idx = P_idx + 1
R_idx = P_idx + 2
while (R_idx < len(A)):
if A[P_idx] + A[Q_idx] > A[R_idx]:
triangle_cnt += R_idx - Q_idx
R_idx += 1
elif Q_idx < R_idx -1:
Q_idx += 1
else:
R_idx += 1
Q_idx += 1
for(let i = A.length-1; i>1; i--){
let tail = i-1;
let head = i-2;
while(A[head]+ A[tail] > A[i]){
let tailT = tail;
let headT = head;
while(A[headT]+ A[tailT] > A[i]){
cnt++;
headT--;
}
head--;
tail--;
}
}
return cnt;
}
Code: 04:17:39 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) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
console.log(A)
let cnt = 0;
for(let i=0; i<A.length-2; i++){
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
}
return cnt;
}
Analysis
expand all
Example tests
1.
0.076 s
OK
stdout:
[ 1, 2, 5, 8, 10, 12 ]
Code: 04:19:02 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
let cnt = 0;
for(let i=0; i<A.length-2; i++){
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
}
return cnt;
}
Code: 04:21:57 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
let cnt = 0;
for(let i=0; i<A.length-2; i++){
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
}
return cnt;
}
Code: 04:22:22 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
let cnt = 0;
A.reduce((cnt,v,i)=>{
let Q = i+1;
let R = i+2;
},0);
for(let i=0; i<A.length-2; i++){
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
}
return cnt;
}
Code: 04:22:33 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
let cnt = 0;
A.reduce((cnt,v,i)=>{
let Q = i+1;
let R = i+2;
while(R < A.length){
}
},0);
for(let i=0; i<A.length-2; i++){
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
}
return cnt;
}
Code: 04:22:43 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
let cnt = 0;
A.reduce((cnt,v,i)=>{
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
},0);
for(let i=0; i<A.length-2; i++){
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
}
return cnt;
}
Code: 04:23:47 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
let cnt = 0;
A.sreduce((cnt,v,i)=>{
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
},0);
for(let i=0; i<A.length-2; i++){
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
}
return cnt;
}
Code: 04:23:57 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
let cnt = 0;
A.slice(0,-2).reduce((cnt,v,i)=>{
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
},0);
for(let i=0; i<A.length-2; i++){
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
}
return cnt;
}
Code: 04:24:08 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
let cnt = 0;
A.slice(0,-2).reduce((cnt,v,i)=>{
let Q = i+1;
let R = i+2;
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
return cnt;
},0);
return cnt;
}
Code: 04:24:27 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) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
return A.slice(0,-2).reduce((cnt,v,i)=>{
let [Q,R] = [i+1,i+2];
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
return cnt;
},0);
return cnt;
}
Analysis
Code: 04:24:31 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) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
return A.slice(0,-2).reduce((cnt,v,i)=>{
let [Q,R] = [i+1,i+2];
while(R < A.length){
if(A[i] + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
return cnt;
},0);
}
Analysis
Code: 04:25:39 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
return A.slice(0,-2).reduce((cnt,v,i)=>{
let [Q,R] = [i+1,i+2];
while(R < A.length){
if(v + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
return cnt;
},0);
}
Code: 04:25:40 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) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
return A.slice(0,-2).reduce((cnt,v,i)=>{
let [Q,R] = [i+1,i+2];
while(R < A.length){
if(v + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if( Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
return cnt;
},0);
}
Analysis
Code: 04:26:00 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) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
return A.slice(0,-2).reduce((cnt,v,i)=>{
let [Q,R] = [i+1,i+2];
while(R < A.length){
if(v + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if(Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
return cnt;
},0);
}
Analysis
Code: 04:26:11 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
return A.slice(0,-2).reduce((cnt,v,i)=>{
let [Q,R] = [i+1,i+2];
while(R < A.length){
if(v + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if(Q < R-1){
Q++;
} else {
R++; Q++;
}
}
return cnt;
},0);
}
Code: 04:26:12 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) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
return A.slice(0,-2).reduce((cnt,v,i)=>{
let [Q,R] = [i+1,i+2];
while(R < A.length){
if(v + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if(Q < R-1){
Q++;
} else {
R++; Q++;
}
}
return cnt;
},0);
}
Analysis
Code: 04:26:20 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) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
return A.slice(0,-2).reduce((cnt,v,i)=>{
let [Q,R] = [i+1,i+2];
while(R < A.length){
if(v + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if(Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
return cnt;
},0);
}
Analysis
Code: 04:26: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) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
return A.slice(0,-2).reduce((cnt,v,i)=>{
let [Q,R] = [i+1,i+2];
while(R < A.length){
if(v + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if(Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
return cnt;
},0);
}
Analysis
Code: 04:26:29 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) {
// write your code in JavaScript (Node.js 8.9.4)
A.sort((a,b)=>a-b);
return A.slice(0,-2).reduce((cnt,v,i)=>{
let [Q,R] = [i+1,i+2];
while(R < A.length){
if(v + A[Q] > A[R]){
cnt += R - Q;
R++;
} else if(Q < R-1){
Q++;
} else {
R++;
Q++;
}
}
return cnt;
},0);
}
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.072 s
OK
1.
0.072 s
OK
1.
0.072 s
OK
1.
0.072 s
OK