Leetcode Problem 2319. Check if Matrix Is X-Matrix

2319. Check if Matrix Is X-Matrix

Leetcode Solutions

Check Diagonals and Non-Diagonal Elements

  1. Iterate over each element in the matrix using two nested loops.
  2. For each element at position (i, j), check if it's on the primary diagonal (i == j) or the secondary diagonal (i + j == n - 1).
  3. If the element is on a diagonal, check that it is non-zero. If any diagonal element is zero, return false.
  4. If the element is not on a diagonal, check that it is zero. If any non-diagonal element is non-zero, return false.
  5. If all checks pass, return true.
UML Thumbnail

Check Diagonals First, Then Validate Non-Diagonal Elements

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...