> ## 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 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 1, [Easy](https://leetcode-py.wisl.dev/catalog/easy.md). Topics: [Array](https://leetcode-py.wisl.dev/catalog/topics/array), [Hash Table](https://leetcode-py.wisl.dev/catalog/topics/hash-table). [View on LeetCode](https://leetcode.com/problems/two-sum/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](https://leetcode-py.wisl.dev/practice/testing.md), 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](https://leetcode-py.wisl.dev/catalog/grind-75.md), [Grind](https://leetcode-py.wisl.dev/catalog/grind.md), [Blind 75](https://leetcode-py.wisl.dev/catalog/blind-75.md), [NeetCode 150](https://leetcode-py.wisl.dev/catalog/neetcode-150.md), [NeetCode 250](https://leetcode-py.wisl.dev/catalog/neetcode-250.md), [NeetCode All](https://leetcode-py.wisl.dev/catalog/neetcode.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.