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

# Find Missing and Repeated Values

> Tested Python solution for LeetCode 2965 with 21 pytest cases. Generate a practice environment with lcpy.

LeetCode 2965, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/find-missing-and-repeated-values/description/).

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

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

## Problem

You are given a 0-indexed 2D integer matrix `grid` of size `n * n` with values in the range `[1, n^2]`. Each integer appears exactly once except `a` which appears twice and `b` which is missing. The task is to find the repeating and missing numbers `a` and `b`.

Return a 0-indexed integer array `ans` of size 2 where `ans[0]` equals to `a` and `ans[1]` equals to `b`.

### Examples

```
Input: grid = [[1,3],[2,2]]
Output: [2,4]
Explanation: Number 2 is repeated and number 4 is missing so the answer is [2,4].
```

```
Input: grid = [[9,1,7],[8,9,2],[3,4,6]]
Output: [9,5]
Explanation: Number 9 is repeated and number 5 is missing so the answer is [9,5].
```

### Constraints

* 2 \<= n == grid.length == grid\[i].length \<= 50
* 1 \<= grid\[i]\[j] \<= n \* n
* For all x that 1 \<= x \<= n \* n there is exactly one x that is not equal to any of the grid members.
* For all x that 1 \<= x \<= n \* n there is exactly one x that is equal to exactly two of the grid members.
* For all x that 1 \<= x \<= n \* n except two of them there is exactly one pair of i, j that 0 \<= i, j \<= n - 1 and grid\[i]\[j] == x.

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n^2)
    # Space: O(n^2)
    def find_missing_and_repeated_values(self, grid: list[list[int]]) -> list[int]:
        n = len(grid)
        counts: dict[int, int] = {}
        repeated = 0
        for row in grid:
            for val in row:
                if val in counts:
                    repeated = val
                counts[val] = counts.get(val, 0) + 1
        total = n * n
        missing = total * (total + 1) // 2 - sum(counts)
        return [repeated, missing]
```

## Complexity

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

## Tags

[NeetCode All](/catalog/neetcode).


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