Codility Task Determine whether a triangle knowledge-city-free
PYTHON CODILITY TASK DETERMINE WHETHER A TRIANDLE CAN BE BUILT FROM A GIVEN SET OF EDGES
def solution(A):
N = len(A)
if N < 3:
return 0 # Not enough elements to form a triplet
A.sort() # Sort the array in ascending order
for i in range(N - 2):
if A[i] + A[i + 1] > A[i + 2]:
return 1 # Triangular triplet found
return 0 # No triangular triplet found
# Example usage:
A1 = [10, 2, 5, 1, 8, 20]
print(solution(A1)) # Output: 1
A2 = [10, 50, 5, 1]
print(solution(A2)) # Output: 0
Post a Comment