Print matrix in diagonal pattern | geeksforgeeks POTD

preview_player
Показать описание

Print matrix in diagonal pattern

Given a square matrix mat[][] of n*n size, the task is to determine the diagonal pattern which is a linear arrangement of the elements of the matrix as depicted in the following example:

Example 1:
Input:
n = 3
mat[][] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output: {1, 2, 4, 7, 5, 3, 6, 8, 9}
Explaination:
Starting from (0, 0): 1,
Move to the right to (0, 1): 2,
Move diagonally down to (1, 0): 4,
Move diagonally down to (2, 0): 7,
Move diagonally up to (1, 1): 5,
Move diagonally up to (1, 2): 3,
Move to the right to (2, 1): 6,
Move diagonally up to (0, 2): 8,
Move diagonally up to (2, 2): 9
There for the output is {1, 2, 4, 7, 5, 3, 6, 8, 9}.

Example 2:
Input:
n = 2
mat[][] = {{1, 2},
{3, 4}}
Output: {1, 2, 3, 4}
Explaination:
Starting from (0, 0): 1,
Move to the right to (0, 1): 2,
Move diagonally down to (1, 0): 3,
Move to the right to (1, 1): 4
There for the output is {1, 2, 3, 4}.
Your Task:
You only need to implement the given function matrixDiagonally() which takes a matrix mat[][] of size n*n as an input and returns a list of integers containing the matrix diagonally. Do not read input, instead use the arguments given in the function.

Disclaimer:
All videos are for educational purposes and use them wisely. Any video may have a slight mistake, please take decisions based on your research. This video is not forcing anything on you.
------------------------------------------------------------------------------------------------------
Join & Connect
Рекомендации по теме
Комментарии
Автор

Bhaiya, It was really good explanation in easiest language. app mujhe help kr skte ho DSA k resources se jisse mujhe problem k liye logic likhna aa jaye?

KULDEEPKUMARPRAJAPATI-lrzp