Files

43 lines
1.0 KiB
Python

# HumanEval/40
# Loki Mode Multi-Agent Solution
# Attempts: 1
# Passed: True
def triples_sum_to_zero(l: list):
"""
triples_sum_to_zero takes a list of integers as an input.
it returns True if there are three distinct elements in the list that
sum to zero, and False otherwise.
>>> triples_sum_to_zero([1, 3, 5, 0])
False
>>> triples_sum_to_zero([1, 3, -2, 1])
True
>>> triples_sum_to_zero([1, 2, 3, 7])
False
>>> triples_sum_to_zero([2, 4, -5, 3, 9, 7])
True
>>> triples_sum_to_zero([1])
False
"""
n = len(l)
if n < 3:
return False
sorted_list = sorted(l)
for i in range(n - 2):
target = -sorted_list[i]
left = i + 1
right = n - 1
while left < right:
current_sum = sorted_list[left] + sorted_list[right]
if current_sum == target:
return True
elif current_sum < target:
left += 1
else:
right -= 1
return False