Skip to main content
Back to problems
Leetcode
Medium
Arrays
Math
Hash Maps
Amazon
Microsoft
Find Missing And Repeated Values

Find the one value that appears twice and the one value that is missing from an n×nn \times n grid containing numbers from $1toton^2$.

Acceptance 0%
Problem Statement

Find Missing and Repeated Values

You are given an n×nn \times n matrix containing integers from $1toton^2$.

Exactly one number appears twice, and exactly one number in the range $1toton^2$ does not appear at all.

Return the repeated value and the missing value.

Notes

  • The matrix contains n2n^2 cells.
  • Every value should be in the range $1toton^2$.
  • There is guaranteed to be exactly one repeated value and exactly one missing value.

Input Format

  • A 2D integer array grid of size n×nn \times n.
  • Each cell contains an integer in the range [1,n2][1, n^2].
  • One value appears twice, one value is absent.

Output Format

Return an array of two integers [repeated, missing].

Constraints

  • 1n501 \le n \le 50 (typical interview-scale formulation)
  • Values are in [1,n2][1, n^2]
  • Exactly one value is repeated and exactly one value is missing
Examples
Sample cases returned by the problem API.

Example 1

Input

grid = [[1,3],[2,2]]

Output

[2,4]

Explanation

The values should be 1, 2, 3, 4. The number 2 appears twice, and 4 is missing.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.