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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Python
Time spent on task 4 minutes
Notes
not defined yet
Task timeline
Code: 16:18:15 UTC,
java,
autosave
Code: 16:18:26 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def next_perm(a):
i = len(a)-1
while i > 0 and a[i-1] >= a[i]:
i -= 1
if i <= 0:
return False
j = len(a)-1
while a[i-1] >= a[j]:
j -= 1
a[i-1], a[j] = a[j], a[i-1]
j = len(a)-1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def solution(A):
if len(A) < 3:
return 0
N = len(A)
_A = sorted(A)
p = [0]*(N-3) + [1]*3
while True:
num = [p[i]*_A[i] for i in range(N) if p[i] != 0]
if num[0]+num[1] > num[2] and num[0]+num[2] > num[1] and num[1]+num[2] > num[0]:
return 1
if not next_perm(p):
break
return 0
Code: 16:18:44 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def next_perm(a):
i = len(a)-1
while i > 0 and a[i-1] >= a[i]:
i -= 1
if i <= 0:
return False
j = len(a)-1
while a[i-1] >= a[j]:
j -= 1
a[i-1], a[j] = a[j], a[i-1]
j = len(a)-1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def solution(A):
if len(A) < 3:
return 0
N = len(A)
_A = sorted(A)
p = [0]*(N-3) + [1]*3
while True:
num = [p[i]*_A[i] for i in range(N) if p[i] > 0]
if num[0]+num[1] > num[2] and num[0]+num[2] > num[1] and num[1]+num[2] > num[0]:
return 1
if not next_perm(p):
break
return 0
Code: 16:19:03 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def next_perm(a):
i = len(a)-1
while i > 0 and a[i-1] >= a[i]:
i -= 1
if i <= 0:
return False
j = len(a)-1
while a[i-1] >= a[j]:
j -= 1
a[i-1], a[j] = a[j], a[i-1]
j = len(a)-1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def solution(A):
if len(A) < 3:
return 0
N = len(A)
_A = sorted(A)
p = [0]*(N-3) + [1]*3
while True:
num = [p[i]*_A[i] for i in range(N) if p[i]*_A[i] > 0]
if num[0]+num[1] > num[2] and num[0]+num[2] > num[1] and num[1]+num[2] > num[0]:
return 1
if not next_perm(p):
break
return 0
Code: 16:19:05 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def next_perm(a):
i = len(a)-1
while i > 0 and a[i-1] >= a[i]:
i -= 1
if i <= 0:
return False
j = len(a)-1
while a[i-1] >= a[j]:
j -= 1
a[i-1], a[j] = a[j], a[i-1]
j = len(a)-1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def solution(A):
if len(A) < 3:
return 0
N = len(A)
_A = sorted(A)
p = [0]*(N-3) + [1]*3
while True:
num = [p[i]*_A[i] for i in range(N) if p[i]*_A[i] > 0]
if num[0]+num[1] > num[2] and num[0]+num[2] > num[1] and num[1]+num[2] > num[0]:
return 1
if not next_perm(p):
break
return 0
Analysis
Code: 16:19:19 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def next_perm(a):
i = len(a)-1
while i > 0 and a[i-1] >= a[i]:
i -= 1
if i <= 0:
return False
j = len(a)-1
while a[i-1] >= a[j]:
j -= 1
a[i-1], a[j] = a[j], a[i-1]
j = len(a)-1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def solution(A):
if len(A) < 3:
return 0
N = len(A)
_A = sorted(A)
p = [0]*(N-3) + [1]*3
while True:
num = [p[i]*_A[i] for i in range(N) if p[i]*_A[i] > 0]
if num[0]+num[1] > num[2] and num[0]+num[2] > num[1] and num[1]+num[2] > num[0]:
return 1
if not next_perm(p):
break
return 0
User test case 1:
[-1, -1, -1]
Analysis
expand all
User tests
1.
0.036 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
Traceback (most recent call last): File "exec.py", line 129, in <module> main() File "exec.py", line 91, in main result = solution( A ) File "/tmp/solution.py", line 42, in solution if num[0]+num[1] > num[2] and num[0]+num[2] > num[1] and num[1]+num[2] > num[0]: IndexError: list index out of range
Code: 16:20:17 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def next_perm(a):
i = len(a)-1
while i > 0 and a[i-1] >= a[i]:
i -= 1
if i <= 0:
return False
j = len(a)-1
while a[i-1] >= a[j]:
j -= 1
a[i-1], a[j] = a[j], a[i-1]
j = len(a)-1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def solution(A):
if len(A) < 3:
return 0
N = len(A)
_A = sorted(A)
p = [0]*(N-3) + [1]*3
while True:
num = [p[i]*_A[i] for i in range(N) if p[i]*_A[i] > 0]
if num[0]+num[1] > num[2] and num[0]+num[2] > num[1] and num[1]+num[2] > num[0]:
return 1
if not next_perm(p):
break
return 0
Code: 16:20:28 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def next_perm(a):
i = len(a)-1
while i > 0 and a[i-1] >= a[i]:
i -= 1
if i <= 0:
return False
j = len(a)-1
while a[i-1] >= a[j]:
j -= 1
a[i-1], a[j] = a[j], a[i-1]
j = len(a)-1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def solution(A):
if len(A) < 3:
return 0
N = len(A)
_A = sorted(A)
p = [0]*(N-3) + [1]*3
while True:
num = [p[i]*_A[i] for i in range(N) if p[i]*_A[i] > 0]
if len(num)
if num[0]+num[1] > num[2] and num[0]+num[2] > num[1] and num[1]+num[2] > num[0]:
return 1
if not next_perm(p):
break
return 0
Code: 16:21:07 UTC,
py,
autosave
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def next_perm(a):
i = len(a)-1
while i > 0 and a[i-1] >= a[i]:
i -= 1
if i <= 0:
return False
j = len(a)-1
while a[i-1] >= a[j]:
j -= 1
a[i-1], a[j] = a[j], a[i-1]
j = len(a)-1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def solution(A):
if len(A) < 3:
return 0
N = len(A)
_A = sorted(A)
p = [0]*(N-3) + [1]*3
while True:
num = [p[i]*_A[i] for i in range(N) if p[i]*_A[i] > 0]
if len(num) <
if num[0]+num[1] > num[2] and num[0]+num[2] > num[1] and num[1]+num[2] > num[0]:
return 1
if not next_perm(p):
break
return 0
Code: 16:21:14 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def next_perm(a):
i = len(a)-1
while i > 0 and a[i-1] >= a[i]:
i -= 1
if i <= 0:
return False
j = len(a)-1
while a[i-1] >= a[j]:
j -= 1
a[i-1], a[j] = a[j], a[i-1]
j = len(a)-1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def solution(A):
if len(A) < 3:
return 0
N = len(A)
_A = sorted(A)
p = [0]*(N-3) + [1]*3
while True:
num = [p[i]*_A[i] for i in range(N) if p[i]*_A[i] > 0]
if len(num) < 3:
return 0
if num[0]+num[1] > num[2] and num[0]+num[2] > num[1] and num[1]+num[2] > num[0]:
return 1
if not next_perm(p):
break
return 0
User test case 1:
[-1, -1, -1]
Analysis
Code: 16:21:21 UTC,
py,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def next_perm(a):
i = len(a)-1
while i > 0 and a[i-1] >= a[i]:
i -= 1
if i <= 0:
return False
j = len(a)-1
while a[i-1] >= a[j]:
j -= 1
a[i-1], a[j] = a[j], a[i-1]
j = len(a)-1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def solution(A):
if len(A) < 3:
return 0
N = len(A)
_A = sorted(A)
p = [0]*(N-3) + [1]*3
while True:
num = [p[i]*_A[i] for i in range(N) if p[i]*_A[i] > 0]
if len(num) < 3:
return 0
if num[0]+num[1] > num[2] and num[0]+num[2] > num[1] and num[1]+num[2] > num[0]:
return 1
if not next_perm(p):
break
return 0
User test case 1:
[-1, -1, -1]
Analysis
Code: 16:21:24 UTC,
py,
final,
score: 
100
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def next_perm(a):
i = len(a)-1
while i > 0 and a[i-1] >= a[i]:
i -= 1
if i <= 0:
return False
j = len(a)-1
while a[i-1] >= a[j]:
j -= 1
a[i-1], a[j] = a[j], a[i-1]
j = len(a)-1
while i < j:
a[i], a[j] = a[j], a[i]
i += 1
j -= 1
return True
def solution(A):
if len(A) < 3:
return 0
N = len(A)
_A = sorted(A)
p = [0]*(N-3) + [1]*3
while True:
num = [p[i]*_A[i] for i in range(N) if p[i]*_A[i] > 0]
if len(num) < 3:
return 0
if num[0]+num[1] > num[2] and num[0]+num[2] > num[1] and num[1]+num[2] > num[0]:
return 1
if not next_perm(p):
break
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.044 s
OK
1.
0.036 s
OK
2.
0.036 s
OK
3.
0.040 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.052 s
OK
2.
0.036 s
OK
3.
0.036 s
OK
4.
0.040 s
OK
5.
0.036 s
OK
6.
0.036 s
OK
1.
0.092 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.184 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.184 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.160 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.144 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