-
Notifications
You must be signed in to change notification settings - Fork 85
/
DiagonalTraversalMatrix.java
41 lines (39 loc) · 1.08 KB
/
DiagonalTraversalMatrix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.*;
public class DiagonalTraversalMatrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int[][] arr = new int[n][n];
int sum = 0;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
arr[i][j]=sc.nextInt();
}
}
for(int l=0;l<=n-1;l++){
int i=0,j=n-1-l;
sum=0;
while(i<n && j<n){
sum += arr[i][j];
i+=1;
j+=1;
}
System.out.print(sum+" ");
}
for(int l=0;l<n-1;l++){
int i=l+1,j=0;
sum=0;
while(i<n && j<n){
sum += arr[i][j];
i+=1;
j+=1;
}
System.out.print(sum+" ");
}
System.out.println("");
}
sc.close();
}
}