2319. Check if Matrix Is X-Matrix LeetCode Solution
In this guide, you will get 2319. Check if Matrix Is X-Matrix LeetCode Solution with the best time and space complexity. The solution to Check if Matrix Is X-Matrix problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Check if Matrix Is X-Matrix solution in C++
- Check if Matrix Is X-Matrix solution in Java
- Check if Matrix Is X-Matrix solution in Python
- Additional Resources

Problem Statement of Check if Matrix Is X-Matrix
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].length
3 <= n <= 100
0 <= grid[i][j] <= 105
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(1)
2319. Check if Matrix Is X-Matrix LeetCode Solution in C++
class Solution {
public:
bool checkXMatrix(vector<vector<int>>& grid) {
const 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) { // in diagonal
if (grid[i][j] == 0)
return false;
} else if (grid[i][j]) { // not in diagonal
return false;
}
return true;
}
};
/* code provided by PROGIEZ */
2319. Check if Matrix Is X-Matrix LeetCode Solution in Java
class Solution {
public boolean checkXMatrix(int[][] grid) {
final 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) { // in diagonal
if (grid[i][j] == 0)
return false;
} else if (grid[i][j] > 0) { // not in diagonal
return false;
}
return true;
}
}
// code provided by PROGIEZ
2319. Check if Matrix Is X-Matrix LeetCode Solution in Python
class Solution:
def checkXMatrix(self, grid: list[list[int]]) -> bool:
n = len(grid)
for i in range(n):
for j in range(n):
if i == j or i + j == n - 1: # in diagonal
if grid[i][j] == 0:
return False
elif grid[i][j]: # not in diagonal
return False
return True
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.