A square matrix is said to be an X-Matrix if both of the following conditions hold:
Given a 2D integer array grid
of size n x n
representing a square matrix, return true
if grid
is an X-Matrix. Otherwise, return false
.
For example, the grid below is an X-Matrix, so return true
[[2,0,0,1],
[0,3,1,0],
[0,5,2,0],
[4,0,0,2]]
In the grid below is not an X-Matrix, return false
[[5,7,0],
[0,3,1],
[0,5,0]]
n == grid.length == grid[i].length
3 <= n <= 100
0 <= grid[i][j] <= 10^5
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:
We're given a grid and we need to check if it's an X-Matrix. The most straightforward way to do this is to examine every single spot in the grid and see if it follows the X-Matrix rules. We'll check each location to determine if it should be a certain value or a zero based on its position in the grid.
Here's how the algorithm would work step-by-step:
def is_x_matrix(grid):
number_of_rows = len(grid)
number_of_columns = len(grid[0])
for row_index in range(number_of_rows):
for column_index in range(number_of_columns):
# Check if the current cell is on either diagonal
is_on_main_diagonal = row_index == column_index
is_on_anti_diagonal = row_index + column_index == number_of_rows - 1
if is_on_main_diagonal or is_on_anti_diagonal:
#Diagonals must not be zero
if grid[row_index][column_index] == 0:
return False
else:
#Off diagonals must be zero
if grid[row_index][column_index] != 0:
return False
# If all checks pass, it's an X-Matrix
return True
To check if a matrix is an X-Matrix efficiently, we focus only on the necessary cells. We verify that the diagonal cells have non-zero values, and all other cells have values of zero, skipping unnecessary checks.
Here's how the algorithm would work step-by-step:
def is_x_matrix(grid):
matrix_size = len(grid)
for row_index in range(matrix_size):
for column_index in range(matrix_size):
#Check diagonal elements
if row_index == column_index or row_index == matrix_size - column_index - 1:
if grid[row_index][column_index] == 0:
return False
# Check non-diagonal elements
else:
if grid[row_index][column_index] != 0:
return False
return True
Case | How to Handle |
---|---|
Null or empty matrix input | Return true if the matrix is null or empty, as an empty matrix technically satisfies the X-matrix condition (vacuously true). |
Non-square matrix (rows != cols) | Return false immediately, since a non-square matrix cannot be an X-Matrix. |
Matrix of size 1x1 | Return true if the single element is non-zero, and false otherwise. |
Matrix with zero on the diagonals | Return false, as an X-Matrix must have non-zero values on the diagonals. |
Matrix with non-zero value off the diagonals | Return false, as an X-Matrix must have zero values off the diagonals. |
Large matrix size (N > 1000) | The solution should still execute efficiently with O(N^2) complexity, as it only iterates through the matrix once. |
Matrix containing negative numbers | The solution is independent of the sign of the numbers, so negative numbers are handled correctly. |
Matrix containing large integer values potentially leading to overflow | The algorithm only involves checking for equality with zero, so integer overflow is not a concern. |