def count_combs(nums, n):
count = 0
num_set = set(nums) # Convert nums list to set for constant-time lookup
for i in nums:
for j in nums:
if i != j and (i + j) / 2 in num_set: # Check if half-sum exists in the set
count += 1
break # No need to continue checking for this element i
return count
if __name__ == "__main__":
n = int(input())
nums = [int(input()) for _ in range(n)]
print(count_combs(nums, n))
Avoid Creating All Combinations: Instead of creating all combinations, we directly iterate through the list nums and compare each element with all the other elements.
Use Sets for Constant-Time Lookup: We convert the nums list into a set num_set to enable constant-time lookup operations.
Optimized Check for Half-Sum: We simplify the logic to check if the half-sum of two elements exists in the set num_set.