Skip to content

2965. Find Missing and Repeated Values #1397

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to identify the repeating and missing numbers in a given n x n matrix where each number in the range [1, n²] appears exactly once except for one number that appears twice and another that is missing.

Approach

  1. Flatten the Grid: Convert the 2D grid into a 1D list of numbers to simplify processing.
  2. Track Counts and Sum: Use a hash map to count occurrences of each number and simultaneously compute the sum of all elements in the grid.
  3. Identify Repeating Number: The number with a count of 2 in the hash map is the repeating number (a).
  4. Calculate Expected Sum: Compute the expected sum of numbers from 1 to n² using the formula for the sum of the first m natural numbers, which is m(m+1)/2.
  5. D…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Mar 6, 2025
Maintainer Author

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Mar 6, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Mar 6, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants