Print Diagonal

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
5 upvotes
Asked in companies
Goldman SachseBay42gearMobilitySystems

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 )
Input Format:
The first line contains an Integer 'T' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case contains two space-separated integers ‘N’ and ‘M’ denoting the number of rows and columns of the matrix respectively.

N’ lines follow. Each of the next ‘N’ lines contains ‘M’ space-separated integers separated by space.
Output Format:
For each test case, return a 2D vector containing all elements of the matrix in a diagonal fashion.

The output of each test case should be printed in a separate line.
Note:
You are not required to print anything, it has already been taken care of. Just implement the function.
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
Hint

Visualize the diagonal pattern and divide it into two separate parts. Can you think of a solution now?

Approaches (1)
Ad Hoc approach

We will try to solve the following problem by dividing it into two parts. In the first part, we will choose each element of the first column as a starting point and print diagonally upward starting at it. In the second part, we will choose each element of the last row as a starting point (except the first element as it has already been processed in the previous part) and print diagonally upward starting at it.

 

Algorithm :


1.   Implement the function ‘IsValid’ which checks whether we are within the boundaries of the matrix or not.

2.   Implement the function ‘traverseDiagonally’, which takes three parameters, 2D vector ‘MAT’, an integer ‘ROW’, and an integer ‘COL’.  

    1.   Initialize the 2D vector ‘ANS’.

    2.   Iterate through 0 to ‘ROW’ (say iterator, ‘k’).

         1.   Push element MAT[k][0] in the 2D vector ‘ANS’.

         2.   Initialize variable ‘i’ as ‘k’ - 1, and initialize variable ‘j’ as 1.  

         3.   While indices ‘i’ and ‘j’ are within the boundaries of the matrix.

              1.   Push element MAT[i][j] in the 2D vector ‘ANS’.

              2.   Decrement the ‘i’, and increment the ‘j’.

    3.   Iterate through 1 to ‘COL’ (say iterator, ‘k’).

         1.   Push element MAT[ROW-1][k] in the 2D vector ‘ANS’.

         2.   Initialize variable ‘i’ as ‘ROW’ - 2, and initialize variable ‘j’ as ‘k’+1.  

         3.   While indices ‘i’ and ‘j’ are within the boundaries of the matrix.

              1.   Push element MAT[i][j] in the 2D vector ‘ANS’.

              2.   Decrement the ‘i’, and increment the ‘j’.
 

Time Complexity

O(N * M) where ‘N’ is the number of rows in the matrix and ‘M’ is the number of columns in the matrix

 

Since we are traversing every element of the matrix only once, hence, time complexity will be O(N*M).

Space Complexity

O(1)

 

Constant extra space is used.

Code Solution
(100% EXP penalty)
Print Diagonal
Full screen
Console