> ## 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.

# Missing Number Python Solution with Tests

> Tested Python solution for LeetCode 268 with 13 pytest cases. Generate a practice environment with lcpy.

LeetCode 268, [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), [Math](https://leetcode-py.wisl.dev/catalog/topics/math), [Binary Search](https://leetcode-py.wisl.dev/catalog/topics/binary-search), [Bit Manipulation](https://leetcode-py.wisl.dev/catalog/topics/bit-manipulation), [Sorting](https://leetcode-py.wisl.dev/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/missing-number/description/).

Generate this problem as a practice environment: tested reference solution, 13 [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 268   # by problem number
lcpy gen -s missing_number   # by problem name
```

## Problem

Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return *the only number in the range that is missing from the array.*

### Examples

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

**Explanation:**

`n = 3` since there are 3 numbers, so all numbers are in the range `[0,3]`. 2 is the missing number in the range since it does not appear in `nums`.

```
Input: nums = [0,1]
Output: 2
```

**Explanation:**

`n = 2` since there are 2 numbers, so all numbers are in the range `[0,2]`. 2 is the missing number in the range since it does not appear in `nums`.

```
Input: nums = [9,6,4,2,3,5,7,0,1]
Output: 8
```

**Explanation:**

`n = 9` since there are 9 numbers, so all numbers are in the range `[0,9]`. 8 is the missing number in the range since it does not appear in `nums`.

### Constraints

* n == nums.length
* 1 \<= n \<= 10^4
* 0 \<= nums\[i] \<= n
* All the numbers of nums are **unique**.

**Follow up:** Could you implement a solution using only `O(1)` extra space complexity and `O(n)` runtime complexity?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def missing_number(self, nums: list[int]) -> int:
        """
        Find the missing number in an array containing n distinct numbers
        in the range [0, n].

        Approach: Use the mathematical formula for sum of consecutive integers.
        The sum of numbers from 0 to n is n*(n+1)/2.
        The missing number = expected_sum - actual_sum.
        """
        n = len(nums)
        expected_sum = n * (n + 1) // 2
        actual_sum = sum(nums)
        return expected_sum - actual_sum
```

## Complexity

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

## Tags

[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.