-
Notifications
You must be signed in to change notification settings - Fork 0
/
Anti-Diagonal.java
56 lines (50 loc) · 1.02 KB
/
Anti-Diagonal.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/******************************************************************************
Write a Java program to create an array of its anti-diagonals from a given square matrix.
Example:
Input :
1 2
3 4
Output:
[
[1],
[2, 3],
[4]
]
*******************************************************************************/
import java.util.*;
class Main{
public static void main(String[] arg){
Scanner x=new Scanner(System.in);
int n=x.nextInt();
int[][] a=new int[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
a[i][j]=x.nextInt();
int j=0,i=0,k=0;
System.out.println("[");
while(j<n){
i=0;
k=j;
while(k>=0){
System.out.print(a[i][k]+" ");
k--;
i++;
}
System.out.println();
j++;
}
i=1;
while(i<n){
j=n-1;
k=i;
while(k<n){
System.out.print(a[k][j]+" ");
k++;
j--;
}
System.out.println();
i++;
}
System.out.println("]");
}
}