Check Identity Matrix

Medium Matrix
Check whether a square matrix is an identity matrix (1s on the diagonal, 0s elsewhere).

Sample input

[[1,0,0],[0,1,0],[0,0,1]]

Sample output

true

Solution

def is_identity(matrix):
    n = len(matrix)
    for i in range(n):
        for j in range(n):
            expected = 1 if i == j else 0
            if matrix[i][j] != expected:
                return False
    return True

print(is_identity([[1, 0, 0], [0, 1, 0], [0, 0, 1]]))
function isIdentity(matrix) {
  const n = matrix.length;
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      const expected = i === j ? 1 : 0;
      if (matrix[i][j] !== expected) return false;
    }
  }
  return true;
}

console.log(isIdentity([[1, 0, 0], [0, 1, 0], [0, 0, 1]]));
public class Main {
    static boolean isIdentity(int[][] matrix) {
        int n = matrix.length;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int expected = (i == j) ? 1 : 0;
                if (matrix[i][j] != expected) return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int[][] matrix = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
        System.out.println(isIdentity(matrix));
    }
}
fun isIdentity(matrix: Array<IntArray>): Boolean {
    val n = matrix.size
    for (i in 0 until n) {
        for (j in 0 until n) {
            val expected = if (i == j) 1 else 0
            if (matrix[i][j] != expected) return false
        }
    }
    return true
}

fun main() {
    val matrix = arrayOf(intArrayOf(1, 0, 0), intArrayOf(0, 1, 0), intArrayOf(0, 0, 1))
    println(isIdentity(matrix))
}
func isIdentity(_ matrix: [[Int]]) -> Bool {
    let n = matrix.count
    for i in 0..<n {
        for j in 0..<n {
            let expected = i == j ? 1 : 0
            if matrix[i][j] != expected { return false }
        }
    }
    return true
}

print(isIdentity([[1, 0, 0], [0, 1, 0], [0, 0, 1]]))
bool isIdentity(List<List<int>> matrix) {
  final n = matrix.length;
  for (var i = 0; i < n; i++) {
    for (var j = 0; j < n; j++) {
      final expected = i == j ? 1 : 0;
      if (matrix[i][j] != expected) return false;
    }
  }
  return true;
}

void main() {
  print(isIdentity([[1, 0, 0], [0, 1, 0], [0, 0, 1]]));
}
#include <iostream>
#include <vector>
using namespace std;

bool isIdentity(const vector<vector<int>>& matrix) {
    int n = matrix.size();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int expected = (i == j) ? 1 : 0;
            if (matrix[i][j] != expected) return false;
        }
    }
    return true;
}

int main() {
    vector<vector<int>> matrix = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
    cout << (isIdentity(matrix) ? "true" : "false") << endl;
    return 0;
}
#include <stdio.h>

int main() {
    int matrix[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
    int n = 3, identity = 1;
    for (int i = 0; i < n && identity; i++) {
        for (int j = 0; j < n; j++) {
            int expected = (i == j) ? 1 : 0;
            if (matrix[i][j] != expected) {
                identity = 0;
                break;
            }
        }
    }
    printf("%s\n", identity ? "true" : "false");
    return 0;
}