Find Missing and Repeated Values
Problem Statement
You are given an n × n grid. It was supposed to hold every number from 1 to n² exactly once. Instead, one number a appears twice and one number b is missing. Every other number appears exactly once.
Return [a, b] — the repeated value first, then the missing value.
A square 2D integer array grid of size n × n, with values in 1..n².
An array of two numbers: [a, b], where a is repeated and b is missing.
The values should be 1..4. 2 appears twice and 4 never appears.
The values should be 1..9. 9 appears twice and 5 never appears.
- • 2 ≤ n == grid.length == grid[i].length ≤ 50
- • 1 ≤ grid[i][j] ≤ n × n
- • Exactly one value in 1..n² is repeated and exactly one is missing; all others appear once.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each k in 1..n², scan the whole grid and count k
Count every value once, then find counts 2 and 0
Treat the grid as one list; swap value v into cell ((v − 1) / n, (v − 1) % n)
Go through k = 1, 2, …, n². For each k, look at every cell and count how often k appears. Count 2 means k is repeated; count 0 means k is missing.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Cyclic Sort translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.