Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Print Diagonal

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
3 upvotes
Asked in companies
eBayGoldman SachsInfosys

Problem statement

You are given a 2D matrix, your task is to return a 2D vector containing all elements of the matrix in a diagonal fashion.

Example:

Following will be the output of the above matrix:

1
5 2
9 6 3
13 10 7 4
14 11 8
15 12
16
Detailed explanation ( Input/output format, Notes, Images )
Constraints :
1 <= T <= 10
1 <= N <= 100
1 <= M <= 100
1 <= mat[i][j] <= 100

Time Limit : 1 sec.
Sample Input 1:
2
4 6
1 2 3 4 5 6
7 8 9 10 11 12 
13 14 15 16 17 18
19 20 21 22 23 24
4 4
1 2 3 4
6 7 8 9
11 12 13 14
16 17 18 19
Sample Output 1:
1
7 2
13 8 3
19 14 9 4
20 15 10 5
21 16 11 6
22 17 12
23 18 
24
1
6 2
11 7 3
16 12 8 4
17 13 9 
18 14
19 
Explanation For Sample Output 1 :
Test Case 1: 

In the above pic, arrow lines represent the diagonals of the matrix which are to be returned.
Test Case 2: 

In the above pic, arrow lines represent the diagonals of the matrix which are to be returned.
Sample Input 2:
2
5 4
1 2 3 4
5 6 7 8
9 10 11 12 
13 14 15 16
17 18 19 20
2 2
8 3
6 1
Sample Output 2:
1
5 2
9 6 3
13 10 7 4
17 14 11 8
18 15 12
19 16
20
8
6 3
1
Full screen
Console