You are viewing a single comment's thread. Return to all comments →
Increment row and column to calculate the left diagnonal, and increment row and decrement column to caculate the right diagonal.
public static int diagonalDifference(List<List<Integer>> arr) { int sumLeftDiagonal = 0; int sumRightDiagonal = 0; for (int row = 0, col = 0; row < arr.get(0).size(); row++, col++) { // System.out.printf("row: %d col: %d, row: %d col: %d %n", row, col, row, arr.size()-1 - col); sumLeftDiagonal += arr.get(row).get(col); sumRightDiagonal += arr.get(row).get(arr.size() - 1 - col); } return Math.abs(sumLeftDiagonal - sumRightDiagonal); }
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 →
Increment row and column to calculate the left diagnonal, and increment row and decrement column to caculate the right diagonal.