Check if Matrix Is X-Matrix
EasyPrompt
A square matrix is said to be an X-Matrix if both of the following conditions hold:
- All the elements in the diagonals of the matrix are non-zero.
- All other elements are 0.
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.
Example 1:
Input: grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]
Output: true
Explanation: Refer to the diagram above.
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
Thus, grid is an X-Matrix.Example 2:
Input: grid = [[5,7,0],[0,3,1],[0,5,0]]
Output: false
Explanation: Refer to the diagram above.
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
Thus, grid is not an X-Matrix.
Constraints:
n == grid.length == grid[i].length3 <= n <= 1000 <= grid[i][j] <= 105
Approaches
2 approaches with complexity analysis and trade-offs.
This approach involves pre-calculating the coordinates of all diagonal and non-diagonal elements and storing them in separate data structures. It then iterates through these stored coordinates to check the matrix values against the X-Matrix conditions. While functionally correct, it's inefficient due to high memory usage.
Algorithm
- Get the dimension
nof the matrix. - Create two lists,
diagonalCoordsandnonDiagonalCoords, to store coordinates. - Iterate from
i = 0ton-1andj = 0ton-1:- If
i == jori + j == n - 1, add the coordinate(i, j)todiagonalCoords. - Otherwise, add
(i, j)tononDiagonalCoords.
- If
- Iterate through each coordinate
(r, c)indiagonalCoords:- If
grid[r][c]is 0, returnfalse.
- If
- Iterate through each coordinate
(r, c)innonDiagonalCoords:- If
grid[r][c]is not 0, returnfalse.
- If
- If all checks pass, return
true.
Walkthrough
The core idea is to separate the identification of element positions from the validation of their values. We first iterate through all possible indices (i, j) of the n x n matrix. For each index, we determine if it lies on the main diagonal (i == j) or the anti-diagonal (i + j == n - 1). We maintain two lists of coordinates: one for diagonal elements and one for non-diagonal elements. After populating these lists, we perform two separate checks:
- Iterate through the list of diagonal coordinates and verify that every corresponding element in the
gridis non-zero. If we find a zero, we immediately returnfalse. - Iterate through the list of non-diagonal coordinates and verify that every corresponding element in the
gridis zero. If we find a non-zero value, we returnfalse. If both checks pass completely, it means the matrix satisfies the X-Matrix properties, and we returntrue.
import java.util.ArrayList;import java.util.List; class Solution { public boolean checkXMatrix(int[][] grid) { int n = grid.length; List<int[]> diagonalCoords = new ArrayList<>(); List<int[]> nonDiagonalCoords = new ArrayList<>(); // Step 1: Populate coordinate lists for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j || i + j == n - 1) { diagonalCoords.add(new int[]{i, j}); } else { nonDiagonalCoords.add(new int[]{i, j}); } } } // Step 2: Check diagonal elements for (int[] coord : diagonalCoords) { if (grid[coord[0]][coord[1]] == 0) { return false; } } // Step 3: Check non-diagonal elements for (int[] coord : nonDiagonalCoords) { if (grid[coord[0]][coord[1]] != 0) { return false; } } return true; }}Complexity
Time
O(n^2). The first nested loop to populate the lists takes O(n^2). The subsequent loops iterate through O(n) and O(n^2) elements respectively. The total time is dominated by O(n^2).
Space
O(n^2). We store coordinates for all `n^2` elements in the matrix across two lists.
Trade-offs
Pros
Separation of concerns: The logic for identifying positions is separate from the logic for validating values.
Easy to understand and debug due to the explicit separation of steps.
Cons
Highly inefficient in terms of space, using O(n^2) extra memory.
Unnecessary overhead of creating and managing lists of coordinates.
Multiple passes over the data (conceptually), which is less efficient than a single pass.
This is the most efficient approach. It involves iterating through the matrix just once. For each element, it simultaneously checks if it's on a diagonal and if its value adheres to the X-Matrix rules. This avoids any extra space and minimizes computation.
Algorithm
- Get the dimension
nof the matrix. - Iterate through each row
ifrom0ton-1. - Inside this loop, iterate through each column
jfrom0ton-1. - Check if the current element
grid[i][j]is on a diagonal by checking the conditioni == j || i + j == n - 1. - If it is on a diagonal, check if
grid[i][j] == 0. If it is, returnfalse. - If it is not on a diagonal, check if
grid[i][j] != 0. If it is, returnfalse. - If the loops complete without returning
false, returntrue.
Walkthrough
The problem can be solved optimally by traversing the matrix a single time. We can use nested loops to visit every cell grid[i][j]. For each cell, we first determine its position relative to the diagonals. An element grid[i][j] is on a diagonal if its row index i is equal to its column index j (main diagonal), or if the sum of its indices i + j equals n - 1 (anti-diagonal). We then apply the two conditions of an X-Matrix:
- If the element is on a diagonal (
i == j || i + j == n - 1): Its value must be non-zero. If we findgrid[i][j] == 0, we can immediately conclude the matrix is not an X-Matrix and returnfalse. - If the element is NOT on a diagonal: Its value must be zero. If we find
grid[i][j] != 0, we again know it's not an X-Matrix and returnfalse. If the loops complete without finding any violations, it means every element satisfies the conditions. Therefore, the matrix is an X-Matrix, and we can returntrue. This method is optimal because every element must be checked in the worst-case scenario, and this approach does so in a single pass with constant extra space.
class Solution { public boolean checkXMatrix(int[][] grid) { int n = grid.length; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { // Check if the element is on a diagonal if (i == j || i + j == n - 1) { // Diagonal elements must be non-zero if (grid[i][j] == 0) { return false; } } else { // Non-diagonal elements must be zero if (grid[i][j] != 0) { return false; } } } } return true; }}Complexity
Time
O(n^2). We iterate through the `n x n` matrix exactly once.
Space
O(1). We only use a few variables for loop indices and the matrix size, not dependent on the input size.
Trade-offs
Pros
Optimal time complexity, as every element must be visited in the worst case.
Optimal space complexity, using only constant extra space.
Concise and efficient implementation.
Cons
No significant cons, as this is the ideal solution for the problem.
Solutions
Solution
public class Solution { public bool CheckXMatrix(int[][] grid) { int n = grid.Length; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j || i + j == n - 1) { if (grid[i][j] == 0) { return false; } } else if (grid[i][j] != 0) { return false; } } } return true; }}class Solution { public boolean checkXMatrix ( int [][] grid ) { int n = grid . length ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < n ; ++ j ) { if ( i == j || i + j == n - 1 ) { if ( grid [ i ][ j ] == 0 ) { return false ; } } else if ( grid [ i ][ j ] != 0 ) { return false ; } } } return true ; } }class Solution { public: bool checkXMatrix ( vector < vector < int >>& grid ) { int n = grid . size (); for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < n ; ++ j ) { if ( i == j || i + j == n - 1 ) { if ( ! grid [ i ][ j ]) { return false ; } } else if ( grid [ i ][ j ]) { return false ; } } } return true ; } };class Solution : def checkXMatrix ( self , grid : List [ List [ int ]]) -> bool : for i , row in enumerate ( grid ): for j , v in enumerate ( row ): if i == j or i + j == len ( grid ) - 1 : if v == 0 : return False elif v : return False return TrueVideo walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.