X-Matrix Checker Lab
In this lab, you will be implementing a function to check if a given square matrix is an X-Matrix. An X-Matrix has the following conditions:
- All the elements in the diagonals of the matrix are non-zero.
- All other elements are 0.
You will write a function checkXMatrix
that takes a 2D integer array grid
of size n x n
representing a square matrix, and returns true
if the given grid
is an X-Matrix. Otherwise, it should return false
.
Function signature
/** * @param {number[][]} grid * @return {boolean} */ var checkXMatrix = function(grid) { };
Example 1:
const grid = [ [2, 0, 0, 1], [0, 3, 1, 0], [0, 5, 2, 0], [4, 0, 0, 2] ]; checkXMatrix(grid); // Returns: true
Explanation: In this example, all the elements in the diagonals are non-zero, and all other elements are 0. Thus, the given grid
is an X-Matrix.
Example 2:
const grid = [ [5, 7, 0], [0, 3, 1], [0, 5, 0] ]; checkXMatrix(grid); // Returns: false
Explanation: In this example, the element at position (0, 1) is non-zero and not part of the diagonal. Hence, the given grid
is not an X-Matrix.
Constraints:
n == grid.length == grid[i].length
3 <= n <= 100
0 <= grid[i][j] <= 10^5
Now that you have an understanding of the problem, go ahead and build the lab according to the main instructions provided in the first message. Make sure to write the evaluation script and set up the test environment properly.