LeetCode 2965, Easy. Topics: Array, Hash Table, Math, Matrix. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 21 parametrized pytest cases, and a playground notebook:
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
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, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026