We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static int diagonalDifference(List<List<Integer>> matrix) {
int primaryDiagonalSum = 0;
int secondaryDiagonalSum = 0;
int n = matrix.size();
for (int i = 0; i < n; i++) {
primaryDiagonalSum += matrix.get(i).get(i); // Left-to-right diagonal
secondaryDiagonalSum += matrix.get(i).get(n - i - 1); // Right-to-left diagonal
}
return Math.abs(primaryDiagonalSum - secondaryDiagonalSum);
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Diagonal Difference
You are viewing a single comment's thread. Return to all comments →