Guest User

Untitled

a guest
Jan 24th, 2020
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static int diagonalDifference(List<List<Integer>> arr) {
  2.  
  3. int size = arr.size();
  4. int mainDiagonalSum = 0;
  5. int theOtherDiagonalSum = 0;
  6. for (int i = 0; i < arr.size(); i++) {
  7. for (int j = 0; j < arr.get(i).size(); j++) {
  8. int current = arr.get(i).get(j);
  9. // check if number is on main diagonal
  10. if (i == j) {
  11. mainDiagonalSum += current;
  12. }
  13. if (i + j == size - 1) {
  14. theOtherDiagonalSum += current;
  15. }
  16. }
  17. }
  18. return Math.abs(mainDiagonalSum - theOtherDiagonalSum);
  19.  
  20. }
RAW Paste Data