Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int diagonalDifference(List<List<Integer>> arr) {
- int size = arr.size();
- int mainDiagonalSum = 0;
- int theOtherDiagonalSum = 0;
- for (int i = 0; i < arr.size(); i++) {
- for (int j = 0; j < arr.get(i).size(); j++) {
- int current = arr.get(i).get(j);
- // check if number is on main diagonal
- if (i == j) {
- mainDiagonalSum += current;
- }
- if (i + j == size - 1) {
- theOtherDiagonalSum += current;
- }
- }
- }
- return Math.abs(mainDiagonalSum - theOtherDiagonalSum);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement