Identity Matrix Check

Given a nonempty rectangular matrix of integers, return true if and only if it is square, every diagonal entry is exactly 1, and every off-diagonal entry is…

Given a nonempty rectangular matrix of integers, return true if and only if it is square, every diagonal entry is exactly 1, and every off-diagonal entry is exactly 0. Return false for every valid nonsquare matrix. Values other than 0 and 1 are allowed but must match the exact rule.

1 to 1000 rows, 1 to 500 columns, and at most 250,000 cells; every row has the same positive length. Every cell is a signed 32-bit integer. The visualizer uses matrices of at most 6 rows and 6 columns; larger valid inputs are execution-only.

Example 1: input matrix = [[1, 0], [0, 1]]; output true. The matrix is square, its diagonal entries are 1, and its off-diagonal entries are 0.

Example 2: input matrix = [[1]]; output true. The only entry is on the diagonal and equals 1.

Example 3: input matrix = [[1, 0, 0], [0, 1, 0]]; output false. A rectangular matrix cannot be an identity matrix.

Category: Arrays. Topic: arrays. Difficulty: Easy. Patterns: math.