Given an M x N matrix find all anti-diagonal elements of the matrix..
input :
the first input will contain values of M, N with separated by spaces..
the second line will contain matrix with M x N dimensions
i.e. input is
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
output:
1
2 5
3 6 9
4 7 10 13
8 11 14
12 15
16
M, N = map(int, input().split())
A = []
for _ in range(M):
A.append(input().split())
for col in range(M):
s_col = col
s_row = 0
while(s_col >= 0 and s_row < N):
print(A[s_row][s_col], end=" ")
s_col -= 1
s_row += 1
print()
for row in range(1, N):
s_row = row
s_col = N - 1
while(s_row < N and s_col >= 0):
print(A[s_row][s_col], end=" ")
s_col -= 1
s_row += 1
print()
Comments
Leave a comment