Level
Find the error
-
def two_sum(nums, target):
-
    seen = {}
-
    for i, num in enumerate(nums):
-
        complement = target - num
-
        if complement in seen:
-
            return [seen[complement], i]
-
    seen[num] = i
-
    return []
-
print(two_sum([2, 7, 11, 15], 9))

LeetCode 1: Two Sum

In this LeetCode-style problem, you need to implement the Two Sum algorithm. The function should find two numbers in the input array that add up to the target. Identify the line that's misplaced and select the correct implementation to fix the logic error. Pay attention to how the dictionary is used to optimize the solution.