You are given a 0-indexed 2D integer matrix grid of size n * n with values in the range [1, n2]. 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 2 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:
2 <= n == grid.length == grid[i].length <= 501 <= grid[i][j] <= n * nx that 1 <= x <= n * n there is exactly one x that is not equal to any of the grid members.x that 1 <= x <= n * n there is exactly one x that is equal to exactly two of the grid members.x that 1 <= x <= n * n except two of them there is exactly one pair of i, j that 0 <= i, j <= n - 1 and grid[i][j] == x.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to this problem is like manually checking every possible number to see if it's the missing or repeated one. We'll compare each number with every other number until we find the pair we're looking for.
Here's how the algorithm would work step-by-step:
def find_missing_and_repeated_values(number_list):
list_length = len(number_list)
for possible_missing_number in range(1, list_length + 1):
counts = {}
repeated_number_found = False
for number in number_list:
if number in counts:
counts[number] += 1
repeated_number = number
repeated_number_found = True
break
else:
counts[number] = 1
# If a repeated number is found after assuming a missing number, return the pair.
if repeated_number_found == True:
missing_number = possible_missing_number
return [repeated_number, missing_number]
# Need to check for repeats, as first loop didn't produce result.
counts = {}
repeated_number = -1
for number in number_list:
if number in counts:
repeated_number = number
break
else:
counts[number] = 1
# Find the actual sum and compare it with expected.
actual_sum = sum(number_list)
expected_sum = (list_length * (list_length + 1)) // 2
missing_number = repeated_number + (expected_sum - actual_sum)
# Find the missing number with the expected sum versus the actual sum
return [repeated_number, missing_number]The trick here is to use the number itself to help us find the missing and repeated values. Instead of brute force searching, we'll cleverly use math to solve the problem directly, avoiding extra checks and comparisons.
Here's how the algorithm would work step-by-step:
def find_missing_and_repeated_values(grid):
grid_size = len(grid)
total_numbers = grid_size * grid_size
# Calculate the sum of numbers from 1 to N.
expected_sum = (total_numbers * (total_numbers + 1)) // 2
actual_sum = 0
actual_sum_of_squares = 0
for row in grid:
for number in row:
actual_sum += number
actual_sum_of_squares += number * number
# Calculate the sum of squares from 1 to N.
expected_sum_of_squares = (total_numbers * (total_numbers + 1) * (2 * total_numbers + 1)) // 6
# This difference gives a relation between missing and repeated numbers.
difference_sum = actual_sum - expected_sum
difference_sum_squares = actual_sum_of_squares - expected_sum_of_squares
# Solve two equations:
# repeatedNumber - missingNumber = difference_sum
# repeatedNumber^2 - missingNumber^2 = difference_sum_squares
sum_values = difference_sum_squares // difference_sum
repeated_number = (difference_sum + sum_values) // 2
missing_number = sum_values - repeated_number
return [repeated_number, missing_number]| Case | How to Handle |
|---|---|
| Null input array | Throw IllegalArgumentException or return null after checking for null input to avoid NullPointerException. |
| Array with single element | Return empty list as no repeated or missing numbers can exist when the size is 1. |
| Array with all identical values | The repeated value will be that identical value, and the missing value can be determined by comparing with the expected sequence. |
| Array with numbers outside the range [1, n] | Return an error or adjust the expected range to match the actual numbers in the array if out of range numbers are allowed. |
| Maximum sized input array | Ensure algorithm's time and space complexity remains acceptable; an in-place solution minimizes space usage. |
| Large n values causing potential integer overflow when summing | Use long data types for sums or consider alternative approaches like using a bit vector if applicable. |
| Array contains only the missing number (repeated number is 1) | The algorithm should correctly identify 1 as the repeated number and the missing number present in the array. |
| Array with negative numbers | Adjust the algorithm to handle negative numbers, potentially by offsetting the input array to a positive range, or return an error if negatives are not allowed. |