Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 0-indexed 2D integer matrix grid of size n * n with values in the range [1, n2]. 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.
Example 1:
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].
Example 2:
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 <= 501 <= grid[i][j] <= n * nx that 1 <= x <= n * n there is exactly one x that is not equal to any of the grid members.x that 1 <= x <= n * n there is exactly one x that is equal to exactly two of the grid members.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.Problem summary: You are given a 0-indexed 2D integer matrix grid of size n * n with values in the range [1, n2]. 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[[1,3],[2,2]]
[[9,1,7],[8,9,2],[3,4,6]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2965: Find Missing and Repeated Values
class Solution {
public int[] findMissingAndRepeatedValues(int[][] grid) {
int n = grid.length;
int[] cnt = new int[n * n + 1];
int[] ans = new int[2];
for (int[] row : grid) {
for (int x : row) {
if (++cnt[x] == 2) {
ans[0] = x;
}
}
}
for (int x = 1;; ++x) {
if (cnt[x] == 0) {
ans[1] = x;
return ans;
}
}
}
}
// Accepted solution for LeetCode #2965: Find Missing and Repeated Values
func findMissingAndRepeatedValues(grid [][]int) []int {
n := len(grid)
ans := make([]int, 2)
cnt := make([]int, n*n+1)
for _, row := range grid {
for _, x := range row {
cnt[x]++
if cnt[x] == 2 {
ans[0] = x
}
}
}
for x := 1; ; x++ {
if cnt[x] == 0 {
ans[1] = x
return ans
}
}
}
# Accepted solution for LeetCode #2965: Find Missing and Repeated Values
class Solution:
def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]:
n = len(grid)
cnt = [0] * (n * n + 1)
for row in grid:
for v in row:
cnt[v] += 1
ans = [0] * 2
for i in range(1, n * n + 1):
if cnt[i] == 2:
ans[0] = i
if cnt[i] == 0:
ans[1] = i
return ans
// Accepted solution for LeetCode #2965: Find Missing and Repeated Values
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2965: Find Missing and Repeated Values
// class Solution {
// public int[] findMissingAndRepeatedValues(int[][] grid) {
// int n = grid.length;
// int[] cnt = new int[n * n + 1];
// int[] ans = new int[2];
// for (int[] row : grid) {
// for (int x : row) {
// if (++cnt[x] == 2) {
// ans[0] = x;
// }
// }
// }
// for (int x = 1;; ++x) {
// if (cnt[x] == 0) {
// ans[1] = x;
// return ans;
// }
// }
// }
// }
// Accepted solution for LeetCode #2965: Find Missing and Repeated Values
function findMissingAndRepeatedValues(grid: number[][]): number[] {
const n = grid.length;
const cnt: number[] = Array(n * n + 1).fill(0);
const ans: number[] = Array(2).fill(0);
for (const row of grid) {
for (const x of row) {
if (++cnt[x] === 2) {
ans[0] = x;
}
}
}
for (let x = 1; ; ++x) {
if (cnt[x] === 0) {
ans[1] = x;
return ans;
}
}
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.