Skip to content

Instantly share code, notes, and snippets.

@Gosilama
Last active May 7, 2020 11:17
Show Gist options
  • Save Gosilama/bd32d4e770b757cc7f479995856243d4 to your computer and use it in GitHub Desktop.
Save Gosilama/bd32d4e770b757cc7f479995856243d4 to your computer and use it in GitHub Desktop.
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
public static int diagonalDifference(List<List<Integer>> arr) {
int primarySum = 0;
int secondarySum = 0;
for (int i = 0; i < arr.size(); i++) {
primarySum += arr.get(i).get(j);
secondarySum += arr.get(i).get(arr.size() - j - 1);
}
return Math.abs(primarySum - secondarySum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment