Print Matrix - Diagonal Zig Zag
Coding Problem Keys
Print Matrix - Diagonal Zig Zag
Problem Statement
An R*C matrix is passed as the input to the program. The program must print
the values in zig-zag order diagonally. (Please refer Example Input/Output
section for more details).
Input Format
The first line contains R and C separated by a space.
Next R lines contain C values separated by a space.
Output
The first line contains all R*C elements in zig-zag order diagonally, with
the elements separated by a space.
Boundary Conditions
2 <= R, C <= 100.
Example Input/Output 1
Input
3 3
1 2 3
4 5 6
7 8 9
Output
1 2 4 7 5 3 6 8 9
Example Input/Output 2
Input
3 7
1 2 3 4 5 6 7
8 9 1 2 3 4 5
6 7 8 9 1 2 3
Output
1 2 8 6 9 3 4 1 7 8 2 5 6 3 9 1 4 7 5 2 3
Note: Max Execution Time Limit: 5000 millisecs
Solution
Programming Language: Python 3 Language
r,c=map(int,input().split()) m=[list(map(int,input().split())) for i in range(r)] s=[[] for i in range(r+c-1)] for i in range(r): for j in range(c): if (i+j)%2==0:s[i+j].insert(0,m[i][j]) else:s[i+j].append(m[i][j]) for i in s: for j in i:print(j,end=' ')
# Published By PKJCODERS(Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.)
Comments