Skip to content

Latest commit

 

History

History
40 lines (34 loc) · 687 Bytes

DiagonalSumSolution.md

File metadata and controls

40 lines (34 loc) · 687 Bytes

Diagonal Sum Solution

Swift

func diagonalSum(of matrix: [[Int]]) -> Int {
    var sum = 0
    for i in 0..<matrix.count {
        sum += matrix[i][i]
        sum += matrix[i][matrix.count - 1 - i]
    }
    return sum
}

Javascript

function diagonalSum(matrix) {
    var sum = 0;
    for (var i = 0; i < matrix.length; i++) {
        sum += matrix[i][i];
        sum += matrix[i][matrix.length - 1 - i]
    }
    return sum
}

Java

public static int diagonalSum(int[][] arr) {
    int sum = 0;
    for (int i = 0; i < arr.length; i++) {
        sum += arr[i][i];
        sum += arr[i][arr.length - 1 - i];
    }
    return sum;
}