Find Missing and Repeated Values - Problem
Find Missing and Repeated Values
Imagine you're auditing a digital inventory system that should contain exactly one instance of each item numbered from
You are given a
• a = the repeated value (appears twice)
• b = the missing value (should be present but isn't)
Goal: Return an array
Example: In a 3×3 grid with values [1,2,3,4,5,6,7,8,9], if we have [1,2,3,4,5,5,7,8,9], then
Imagine you're auditing a digital inventory system that should contain exactly one instance of each item numbered from
1 to n². However, due to a system glitch, one item appears twice while another item is completely missing.You are given a
n × n integer matrix grid where each cell should contain a unique value from 1 to n². Your task is to identify:• a = the repeated value (appears twice)
• b = the missing value (should be present but isn't)
Goal: Return an array
[a, b] where a is the repeated value and b is the missing value.Example: In a 3×3 grid with values [1,2,3,4,5,6,7,8,9], if we have [1,2,3,4,5,5,7,8,9], then
a=5 (repeated) and b=6 (missing). Input & Output
example_1.py — Basic Case
$
Input:
grid = [[1,3],[2,2]]
›
Output:
[2,4]
💡 Note:
In this 2×2 grid, we should have numbers 1,2,3,4. Number 2 appears twice (repeated) and number 4 is missing.
example_2.py — Larger Grid
$
Input:
grid = [[9,1,7],[8,9,2],[3,4,6]]
›
Output:
[9,5]
💡 Note:
In this 3×3 grid, we should have numbers 1-9. Number 9 appears twice (at positions [0,0] and [1,1]), and number 5 is missing.
example_3.py — Edge Case
$
Input:
grid = [[1]]
›
Output:
[1,1] is impossible since n≥2. Minimum case: grid = [[2,2],[2,3]] → [2,1]
💡 Note:
For a 2×2 grid with values [2,2,2,3], number 2 appears three times but problem states exactly one repeat, so this violates constraints. Valid minimum: [[2,2],[1,3]] has repeated=2, missing=4.
Constraints
- 2 ≤ n ≤ 1000
- 1 ≤ grid[i][j] ≤ n2
- The grid contains exactly one repeated number and exactly one missing number
- All other numbers from 1 to n2 appear exactly once
Visualization
Tap to expand
Understanding the Visualization
1
Start Roll Call
Begin with empty attendance sheet (hash table)
2
Mark Present
For each student number called, mark them present
3
Catch Duplicate
If someone already marked present answers again, found our duplicate!
4
Find Absent
Check attendance sheet to see who never answered
5
Report Results
Return [duplicate_student, absent_student]
Key Takeaway
🎯 Key Insight: Hash tables excel at detecting duplicates because lookups are O(1). The moment we see a number twice, we've found our answer!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code