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:
def solution(A)
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 Python
Time spent on task 20 minutes
Notes
not defined yet
Task timeline
Code: 01:34:59 UTC,
java,
autosave
Code: 01:38:00 UTC,
py,
autosave
Code: 01:39:57 UTC,
py,
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.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
[1, 2, 5, 8, 10, 20]
example1
example, answer is zero, length=4
example, answer is zero, length=4
✘
RUNTIME ERROR
tested program terminated with exit code 1
tested program terminated with exit code 1
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
[1, 5, 10, 50]
Code: 01:41:06 UTC,
py,
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.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
[20, 10, 8, 5, 2, 1]
example1
example, answer is zero, length=4
example, answer is zero, length=4
✘
RUNTIME ERROR
tested program terminated with exit code 1
tested program terminated with exit code 1
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
[50, 10, 5, 1]
Code: 01:41:50 UTC,
py,
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.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
[20, 10, 8, 5, 2, 1] 0 20 1 10 2 8 3 5 4 2 5 1
example1
example, answer is zero, length=4
example, answer is zero, length=4
✘
RUNTIME ERROR
tested program terminated with exit code 1
tested program terminated with exit code 1
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
[50, 10, 5, 1] 0 50 1 10 2 5 3 1
Code: 01:42:28 UTC,
py,
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.040 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
6 [20, 10, 8, 5, 2, 1] 0 20 1 10 2 8 3 5 4 2 5 1
example1
example, answer is zero, length=4
example, answer is zero, length=4
✘
RUNTIME ERROR
tested program terminated with exit code 1
tested program terminated with exit code 1
1.
0.040 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
4 [50, 10, 5, 1] 0 50 1 10 2 5 3 1
Code: 01:42:38 UTC,
py,
autosave
Code: 01:43:15 UTC,
py,
autosave
Code: 01:45:28 UTC,
py,
autosave
Code: 01:45:58 UTC,
py,
autosave
Code: 01:48:29 UTC,
py,
autosave
Code: 01:48:59 UTC,
py,
autosave
Code: 01:49:29 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
while now_num + 2 > length_A-1:
print(sorted_A[now_num], sorted_A[now_num], sorted_A[now_num])
now_num += 1
Code: 01:49:37 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
while now_num + 2 > length_A-1:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
now_num += 1
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.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.
example1
example, answer is zero, length=4
example, answer is zero, length=4
✘
RUNTIME ERROR
tested program terminated with exit code 1
tested program terminated with exit code 1
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.
Code: 01:49:50 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
while now_num + 2 > length_A-1:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
now_num += 1
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.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.
example1
example, answer is zero, length=4
example, answer is zero, length=4
✘
RUNTIME ERROR
tested program terminated with exit code 1
tested program terminated with exit code 1
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.
Code: 01:50:09 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
while now_num + 2 length_A-1:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
now_num += 1
Code: 01:50:11 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
while now_num + 2 < length_A-1:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
now_num += 1
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.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
20 10 8 10 8 5 8 5 2
example1
example, answer is zero, length=4
example, answer is zero, length=4
✘
RUNTIME ERROR
tested program terminated with exit code 1
tested program terminated with exit code 1
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
50 10 5
Code: 01:50:23 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A-1:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
now_num += 1
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.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
[20, 10, 8, 5, 2, 1] 20 10 8 10 8 5 8 5 2
example1
example, answer is zero, length=4
example, answer is zero, length=4
✘
RUNTIME ERROR
tested program terminated with exit code 1
tested program terminated with exit code 1
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
[50, 10, 5, 1] 50 10 5
Code: 01:50:34 UTC,
py,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
now_num += 1
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.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
[20, 10, 8, 5, 2, 1] 20 10 8 10 8 5 8 5 2 5 2 1
example1
example, answer is zero, length=4
example, answer is zero, length=4
✘
RUNTIME ERROR
tested program terminated with exit code 1
tested program terminated with exit code 1
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Invalid result type, int expected, <class 'NoneType'> found.stdout:
[50, 10, 5, 1] 50 10 5 10 5 1
Code: 01:50:59 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
now_num += 1
Code: 01:51:15 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if s
now_num += 1
Code: 01:51:41 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num]
now_num += 1
Code: 01:51:53 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num]
now_num += 1
return 0
Code: 01:52:12 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num]
now_num += 1
return 0
Code: 01:52:42 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num] + sorted_A[now_num + 1] > sorted_A[now_num+ + 2]
now_num += 1
return 0
Code: 01:53:03 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num] + sorted_A[now_num + 1] > sorted_A[now_num+ + 2] and
sorted_A[now_num + 1] + sorted_A[now_num + 2] > sorted_A[now_num] and
sorted_A[now_num] + sorted_A[now_num + 1] > sorted_A[now_num+ + 2]
now_num += 1
return 0
Code: 01:53:17 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num] + sorted_A[now_num + 1] > sorted_A[now_num+ + 2] and
sorted_A[now_num + 1] + sorted_A[now_num + 2] > sorted_A[now_num] and
sorted_A[now_num] + sorted_A[now_num + 2] > sorted_A[now_num+ + 1]
now_num += 1
return 0
Code: 01:53:27 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num] + sorted_A[now_num + 1] > sorted_A[now_num+ + 2] and
sorted_A[now_num + 1] + sorted_A[now_num + 2] > sorted_A[now_num] and
sorted_A[now_num] + sorted_A[now_num + 2] > sorted_A[now_num+ + 1]:
now_num += 1
return 0
Code: 01:53:38 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num] + sorted_A[now_num + 1] > sorted_A[now_num+ + 2] and sorted_A[now_num + 1] + sorted_A[now_num + 2] > sorted_A[now_num] and sorted_A[now_num] + sorted_A[now_num + 2] > sorted_A[now_num+ + 1]:
now_num += 1
return 0
Code: 01:53:52 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num] + sorted_A[now_num + 1] > sorted_A[now_num+ + 2] and sorted_A[now_num + 1] + sorted_A[now_num + 2] > sorted_A[now_num] and sorted_A[now_num] + sorted_A[now_num + 2] > sorted_A[now_num+ + 1]:
return 1
now_num += 1
return 0
User test case 1:
[50, 10, 5]
Analysis
expand all
Example tests
1.
0.036 s
OK
stdout:
[20, 10, 8, 5, 2, 1] 20 10 8 10 8 5
1.
0.036 s
OK
stdout:
[50, 10, 5, 1] 50 10 5 10 5 1
expand all
User tests
1.
0.036 s
OK
function result: 0
function result: 0
stdout:
[50, 10, 5] 50 10 5
Code: 01:54:06 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
#print(sorted_A)
while now_num + 2 < length_A:
print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num] + sorted_A[now_num + 1] > sorted_A[now_num+ + 2] and sorted_A[now_num + 1] + sorted_A[now_num + 2] > sorted_A[now_num] and sorted_A[now_num] + sorted_A[now_num + 2] > sorted_A[now_num+ + 1]:
return 1
now_num += 1
return 0
Code: 01:54:13 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
#print(sorted_A)
while now_num + 2 < length_A:
#print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num] + sorted_A[now_num + 1] > sorted_A[now_num+ + 2] and sorted_A[now_num + 1] + sorted_A[now_num + 2] > sorted_A[now_num] and sorted_A[now_num] + sorted_A[now_num + 2] > sorted_A[now_num+ + 1]:
return 1
now_num += 1
return 0
User test case 1:
[50, 10, 5]
Analysis
Code: 01:54:18 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
#print(sorted_A)
while now_num + 2 < length_A:
#print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num] + sorted_A[now_num + 1] > sorted_A[now_num+ + 2] and sorted_A[now_num + 1] + sorted_A[now_num + 2] > sorted_A[now_num] and sorted_A[now_num] + sorted_A[now_num + 2] > sorted_A[now_num+ + 1]:
return 1
now_num += 1
return 0
User test case 1:
[50, 10, 5]
Analysis
Code: 01:54:21 UTC,
py,
final,
score: 
100
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(A):
length_A = len(A)
sorted_A = sorted(A, reverse=True)
now_num = 0
#print(sorted_A)
while now_num + 2 < length_A:
#print(sorted_A[now_num], sorted_A[now_num+1], sorted_A[now_num+2])
if sorted_A[now_num] + sorted_A[now_num + 1] > sorted_A[now_num+ + 2] and sorted_A[now_num + 1] + sorted_A[now_num + 2] > sorted_A[now_num] and sorted_A[now_num] + sorted_A[now_num + 2] > sorted_A[now_num+ + 1]:
return 1
now_num += 1
return 0
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N*log(N))
expand all
Correctness tests
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
expand all
Performance tests
1.
0.048 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.088 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.168 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.212 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.176 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.152 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.036 s
OK
5.
0.036 s
OK
6.
0.036 s
OK