# Sort a 2D vector diagonally using Map Data Structure

Given a 2D vector mat[][] of integers. The task is to sort the elements of the vectors diagonally from top-left to bottom-right in increasing order.

Examples:

```Input: mat[][] =
{{9, 4, 2},
{7, 4, 6},
{2, 3, 3}}
Output:
3 4 2
3 4 6
2 7 9
Explanation:
There are 5 diagonals in this matrix:
1. {2} - No need to sort
2. {7, 3} - Sort - {3, 7}
3. {9, 4, 3} - Sort - {3, 4, 9}
4. {4, 6} - Already sorted
5. {2} - No need to sort

Input: mat[][] =
{{ 4, 3, 2, 1 },
{ 3, 2, 1, 0 },
{ 2, 1, 1, 0 },
{ 0, 1, 2, 3 }}
Output:
1 0 0 1
1 2 1 2
1 2 3 3
0 2 3 4
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. All elements in the same diagonal have the same index difference i – j where i is the row number and j is the column number. So we can use a map to store every diagonal at index i – j.
2. Now we can sort every index of the map using the inbuilt function.
3. Now in the original matrix, we can insert every diagonal of a matrix stored in map.
4. Finally, we can print the Matrix.

Below is the implementation of the above approach:

 `// C++ implementation to sort the ` `// diagonals of the matrix ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to sort the ` `// diagonal of the matrix ` `void` `SortDiagonal(``int` `mat[4][4],  ` `                  ``int` `m, ``int` `n) ` `{ ` `    ``// Map to store every diagonal  ` `    ``// in different indices here  ` `    ``// elements of same diagonal  ` `    ``// will be stored in same index ` `    ``unordered_map<``int``, vector<``int``> > mp; ` ` `  `    ``for` `(``int` `i = 0; i < m; i++) ` `    ``{ ` `        ``for` `(``int` `j = 0; j < n; j++) ` `        ``{ ` `            ``// Storing diagonal elements  ` `            ``// in map ` `            ``mp[i - j].push_back(mat[i][j]); ` `        ``} ` `    ``} ` ` `  `    ``// To sort each diagonal in  ` `    ``// ascending order ` `    ``for` `(``int` `k = -(n - 1); k < m; k++) ` `    ``{ ` `        ``sort(mp[k].begin(), ` `             ``mp[k].end()); ` `    ``} ` ` `  `    ``// Loop to store every diagonal  ` `    ``// in ascending order ` `    ``for` `(``int` `i = m - 1; i >= 0; i--) ` `    ``{ ` `        ``for` `(``int` `j = n - 1; j >= 0; j--) ` `        ``{ ` `            ``mat[i][j] = mp[i - j].back(); ` `            ``mp[i - j].pop_back(); ` `        ``} ` `    ``} ` ` `  `    ``// Loop to print the matrix ` `    ``for` `(``int` `i = 0; i < m; i++) { ` `        ``for` `(``int` `j = 0; j < n; j++) ` `            ``cout << mat[i][j] << ``" "``; ` `        ``cout << endl; ` `    ``} ` `} ` ` `  `// Driven Code ` `int` `main() ` `{ ` `    ``int` `arr[4][4] = { { 4, 3, 2, 1 }, ` `                    ``{ 3, 2, 1, 0 }, ` `                    ``{ 2, 1, 1, 0 }, ` `                    ``{ 0, 1, 2, 3 } }; ` ` `  `    ``// Sort the Diagonals ` `    ``SortDiagonal(arr, 4, 4); ` ` `  `    ``return` `0; ` `} `

Output:
```1 0 0 1
1 2 1 2
1 2 3 3
0 2 3 4
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :