Advertisement
a4ary4n

Untitled

Oct 13th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.74 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class AntiDiagonalMatrices {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner sc = new Scanner(System.in);
  7.        
  8.         System.out.print("Enter number of rows: ");
  9.         int n = sc.nextInt();
  10.         int[][] arr = new int[n][n];
  11.         for (int i = 0; i < n; i++) {
  12.             for (int j = 0; j < n; j++) {
  13.                 arr[i][j] = sc.nextInt();
  14.             }
  15.         }
  16.        
  17.         ArrayList<Integer>[] nondiag = new ArrayList[2*n - 1];
  18.         for (int i = 0; i < 2*n-1; i++) {
  19.             nondiag[i] = new ArrayList<Integer>();
  20.         }
  21.        
  22.         for (int i = 0; i < n; i++) {
  23.             for (int j = 0; j < n; j++) {
  24.                 nondiag[i+j].add(arr[i][j]);
  25.             }
  26.         }
  27.        
  28.         System.out.println();
  29.         for (int i = 0; i < 2*n-1; i++) {
  30.             System.out.println(nondiag[i]);
  31.         }
  32.        
  33.         sc.close();
  34.  
  35.     }
  36.  
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement