> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Two Sum Python Solution with Tests

> Tested Python solution for LeetCode 1 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 1, Easy. Topics: Array, Hash Table. [View on LeetCode](https://leetcode.com/problems/two-sum/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1   # by problem number
lcpy gen -s two_sum   # by problem name
```

## Problem

Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

### Examples

```
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
```

**Explanation:** Because nums\[0] + nums\[1] == 9, we return \[0, 1].

```
Input: nums = [3,2,4], target = 6
Output: [1,2]
```

```
Input: nums = [3,3], target = 6
Output: [0,1]
```

### Constraints

* 2 \<= nums.length \<= 10^4
* -10^9 \<= nums\[i] \<= 10^9
* -10^9 \<= target \<= 10^9
* Only one valid answer exists.

**Follow-up:** Can you come up with an algorithm that is less than O(n^2) time complexity?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/two_sum/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/two_sum/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def two_sum(self, nums: list[int], target: int) -> list[int]:
        seen: dict[int, int] = {}
        answers: list[list[int]] = []

        for i, num in enumerate(nums):
            complement = target - num
            if complement in seen:
                answer = [seen[complement], i]
                answers.append(answer)
            seen[num] = i

        if len(answers) > 1:
            raise ValueError(f"Found {len(answers)} answers in the solution: {answers}")

        return answers[0] if answers else []
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(n)  |

## Tags

[Grind 75](/catalog/grind-75), [Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
