Leetcode 2965. Find Missing and Repeated Values
Table of Contents
Problem Informations
- Problem Index: 2965
- Problem Link: Find Missing and Repeated Values
- Topics: Array, Hash Table, Math, Matrix
Problem Description
You are given a 0-indexed 2D integer matrix grid
of size with values in the range . Each integer appears exactly once except a
which appears twice and b
which is missing. The task is to find the repeating and missing numbers a
and b
.
Return a 0-indexed integer array ans
of size where ans[0]
equals to a
and ans[1]
equals to b
.
Example 1:
Input: grid = [[1,3],[2,2]]
Output: [2,4]
Explanation: Number 2 is repeated and number 4 is missing so the answer is [2,4].
Example 2:
Input: grid = [[9,1,7],[8,9,2],[3,4,6]]
Output: [9,5]
Explanation: Number 9 is repeated and number 5 is missing so the answer is [9,5].
Constraints:
- For all that there is exactly one that is not equal to any of the grid members.
- For all that there is exactly one that is equal to exactly two of the grid members.
- For all that except two of them there is exactly one pair of that and .
Intuition
The problem presents a scenario where we have a square matrix grid filled with integers ranging from 1 to . Within this matrix, all integers appear exactly once, except for one integer which appears twice, and another integer which is missing entirely. The goal is to identify the integer that repeats (denoted as a
) and the integer that is missing (b
). Given the constraints, this problem can be efficiently solved by utilizing a counting mechanism to keep track of the encountered numbers while traversing the matrix. By doing so, not only can we detect the repeating number, but we can also establish which number is absent by omission.
Approach
The approach involves a straightforward traversal and checking mechanism:
Initialization:
- Start by declaring an integer
repeated_num
to store the repeating number once it’s detected. - Determine the size of the grid
len
, which reflects both the dimensions of the matrix since it is square (i.e., ).
- Start by declaring an integer
Tracking Occurrences:
- To keep track of which numbers have appeared, use a boolean vector
used
with a length of , initialized tofalse
. This vector acts like a hash map for numbers from 1 to .
- To keep track of which numbers have appeared, use a boolean vector
Traversing the Grid:
- Iterate over each element in the grid. For each number
num
, check if it has been marked astrue
inused
:- If
num
has already been encountered (i.e.,used[num]
istrue
), then this number is the repeated one. Assign it torepeated_num
. - Otherwise, mark this
num
astrue
in theused
array, indicating it has been seen.
- If
- Iterate over each element in the grid. For each number
Identifying the Missing Number:
- After processing the entire grid, traverse the
used
vector starting from 1 to . The indexi
whereused[i]
remainsfalse
identifies the missing number, as it was never encountered in the grid.
- After processing the entire grid, traverse the
Return the Result:
- Once both the repeated and the missing numbers are identified, return them as a vector in the order .
This structured approach ensures that we efficiently find both the repeating and missing numbers in a single pass through the grid, followed by a simple scan for the missing number. The use of a boolean array is pivotal as it allows constant time checking and marking operations, crucial for adhering to performance constraints.
Code
C++
class Solution {
public:
vector<int> findMissingAndRepeatedValues(vector<vector<int>>& grid) {
// 'repeated_num' will store the value that appears twice.
int repeated_num;
int len = grid.size(); // 'len' is the size of the grid, since it's square matrix.
// 'used' is a vector to track numbers that have been encountered.
vector<bool> used(len * len + 1, false);
// Traverse each element in the 2D grid.
for (vector<int>& row : grid) {
for (int num : row) {
// Check if the number has already appeared; if yes, assign it to 'repeated_num'.
if (used[num]) {
repeated_num = num;
}
used[num] = true; // Mark the number as encountered.
}
}
// Find the missing number by checking the 'used' vector.
for (int i = 1; i < len * len + 1; i++) {
if (!used[i]) {
return {repeated_num, i};
}
}
// Default return if logic fails, though per constraints should not occur.
return {-1, -1};
}
};
Python
class Solution:
def findMissingAndRepeatedValues(self, grid):
# 'repeated_num' will store the value that appears twice.
repeated_num = None
len_grid = len(grid) # 'len' is the size of the grid, since it's square matrix.
# 'used' is a list to track numbers that have been encountered.
used = [False] * (len_grid * len_grid + 1)
# Traverse each element in the 2D grid.
for row in grid:
for num in row:
# Check if the number has already appeared; if yes, assign it to 'repeated_num'.
if used[num]:
repeated_num = num
used[num] = True # Mark the number as encountered.
# Find the missing number by checking the 'used' list.
for i in range(1, len_grid * len_grid + 1):
if not used[i]:
return [repeated_num, i]
# Default return if logic fails, though per constraints should not occur.
return [-1, -1]
Complexity
Time complexity: The time complexity is , where is the size of one dimension of the grid. This is because the algorithm iterates over each element in the grid during the first nested loop to mark the used numbers. The second loop runs at most times to find the missing number, which is bounded by the same factor . Thus, the overall time complexity is .
Space complexity: The space complexity is . This arises from the ‘used’ vector, which is of size to accommodate all numbers ranging from to . Other variables use constant space. Therefore, the dominant space requirement is for the ‘used’ vector, resulting in a space complexity of .
Disclaimer: All reference materials on this website are sourced from the internet and are intended for learning purposes only. If you believe any content infringes upon your rights, please contact me at csnote.cc@gmail.com, and I will remove the relevant content promptly.
Feedback Welcome: If you notice any errors or areas for improvement in the articles, I warmly welcome your feedback and corrections. Your input will help this blog provide better learning resources. This is an ongoing process of learning and improvement, and your suggestions are valuable to me. You can reach me at csnote.cc@gmail.com.