You are given a square integer matrix grid of size n x n that is intended to contain every value from 1 to n2 exactly once. However, one value appears twice in grid, and one value from the range 1 to n2 does not appear at all.
Return an array of length 2 where the first element is the repeated value and the second element is the missing value.
Constraints:
2≤ n == grid.length == grid[i].length ≤50
1≤ grid[i][j] ≤n⋅n
For all x such that 1≤ x ≤n⋅n, there exists exactly one x that is not equal to any value in grid
For all x such that 1≤ x ≤n⋅n, there exists exactly one x that is equal to exactly 2 values in grid
For all x such that 1≤ x ≤n⋅n except two of them, there exists exactly one pair i, j such that 0≤ i, j ≤n−1 and grid[i][j] == x
Input: grid = [[1,2],[4,4]]-List[List[int]]
Output: [4,3]-List[int]
Explanation: The matrix should contain all values from 1 to 4. The value 4 appears twice, and the value 3 is missing.
Input: grid = [[1,2,3],[4,5,6],[7,8,8]]-List[List[int]]
Output: [8,9]-List[int]
Explanation: The matrix should contain all values from 1 to 9. The value 8 is repeated, and 9 does not appear.
Input: grid = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,15]]-List[List[int]]
Output: [15,16]-List[int]
Explanation: The matrix should contain all values from 1 to 16. The value 15 occurs twice, and 16 is missing.
Input: grid = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,24]]-List[List[int]]
Output: [24,25]-List[int]
Explanation: The matrix should contain all values from 1 to 25. The value 24 is duplicated, and 25 is missing.
Now, let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem: