Given a MxN matrix,write a program to print all Anti-Diagonals elements of matrix
Input
The first line of input will contain a M, N values separated by space.
The second line will contain matrix A of dimensions MxN.
Output
The output should contain anti-diagonal elements separated by a line.
Explanation
For example, if M = 4, N = 4
Matrix A:
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
so the output should be
1
2 5
3 6 9
4 7 10 13
8 11 14
12 15
16
A = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
M = len(A)
N = len(A[0])
print("%d %d"%(M,N))
for col in range(N):
scol = col
srow = 0
while(scol >= 0 and srow < N):
print(A[srow][scol], end = " ")
scol = scol-1
srow = srow+1
print("")
for row in range(1, N):
srow = row
scol = N - 1
while(srow < N and scol >= 0):
print(A[srow][scol],end=" ")
scol = scol-1
srow = srow+1
print("")
Python Output:
Comments
Leave a comment