Skip to content
Related Articles
Sort a 2D vector diagonally using Map Data Structure
• Last Updated : 21 May, 2021

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 ```

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:

## CPP

 `// 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,``                  ``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, 3, 2, 1 },``                    ``{ 3, 2, 1, 0 },``                    ``{ 2, 1, 1, 0 },``                    ``{ 0, 1, 2, 3 } };` `    ``// Sort the Diagonals``    ``SortDiagonal(arr, 4, 4);` `    ``return` `0;``}`

## Javascript

 ``
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up