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 void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. int[][] mat = {
  14. {1, 2, 2, 1},
  15. {4, 3, 3, 4},
  16. {2, 3, 3, 2}
  17. };
  18.  
  19. //assume the matrix is square
  20. int rows = mat.length, columns = mat[0].length;
  21.  
  22. boolean symmetric = true;
  23. for(int r = 0; r < rows && symmetric; r++){
  24. //now declare two pointers one from left and one from right
  25. int left = 0, right = columns - 1;
  26.  
  27. while (left < right){
  28. if(mat[r][left] != mat[r][right]){
  29. symmetric = false;
  30. break;
  31. }
  32. right--;
  33. left++;
  34. }
  35. }
  36.  
  37. System.out.println(symmetric? "The matrix is symmetric." : "The matrix isn't symmetric.");
  38. }
  39. }
Success #stdin #stdout 0.04s 2184192KB
stdin
Standard input is empty
stdout
The matrix is symmetric.