fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static int find(List<List<Integer>> matrix) {
  11. int sum1 = 0;
  12. int sum2 = 0;
  13. for (int i = 0; i < matrix.size(); i++) {
  14. sum1 += matrix.get(i).get(i);
  15. sum2 += matrix.get(i).get(matrix.size() - 1 - i);
  16. }
  17. return Math.abs(sum1 - sum2);
  18. }
  19. public static void main (String[] args) throws java.lang.Exception
  20. {
  21. Scanner sc = new Scanner(System.in);
  22.  
  23. int n = sc.nextInt();
  24. List<List<Integer>> matrix = new ArrayList<>();
  25. for (int i = 0; i < n; i++) {
  26. List<Integer> row = new ArrayList<>();
  27. for (int j = 0; j < n; j++) {
  28. row.add(sc.nextInt());
  29. }
  30. matrix.add(row);
  31. }
  32.  
  33. int absoluteDiff = find(matrix);
  34.  
  35. System.out.println(absoluteDiff);
  36.  
  37. sc.close();
  38. }
  39. }
Success #stdin #stdout 0.13s 45160KB
stdin
3
11 2 4
4 5 6
10 8 -12
stdout
15