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

**Examples:**

Input:arr[][] = { { 10, 2, 3 }, { 4, 5, 6 }, {7, 8, 9 } }

Output:

10 2 0

4 9 0

0 0 5

Input:arr[][] = { { 10, 2, 43 }, { 40, 5, 16 }, { 71, 8, 29 }, {1, 100, 5} }

Output:

29 2 0

8 10 0

71 5 5

0 0 0

**Approach:**

**Observations:**

The above images shows the difference between the column index and row index at each cell. The cells having same difference from top-left to bottom-down cell forms a diagonal.

Below are the steps to sort diagonal in decreasing order:

- Store the diagonal element with positive difference in one Array of Vectors(say
**Pos[]**) such that elements at the cell having difference(say**a**) is stored at index**a**of**Pos[]**array. - Store the diagonal element with negative difference in another Array of Vectors(say
**Neg[]**) such that elements at the cell having difference(say**-b**) is stored at index**abs(-b) = b**of**Neg[]**array. - Sort both the Array of Vectors increasing order.
- Traverse the given 2D vector and updated the value at current cell with value stored in Pos[] and Neg[] array.
- If difference between column and row index(say
**d**) is positive, then updated the value from Pos[d] array and remove the last elment as:d = i - j arr[i][j] = Pos[d][Pos.size()-1] Pos[d].pop_back()

- If difference between column and row index(say
**d**) is negative, then updated the value from Neg[d] array and remove the last element as:d = j - i arr[i][j] = Neg[d][Neg.size()-1] Neg[d].pop_back()

- If difference between column and row index(say

Below is the implementation of the above approach:

`// C++ program to sort the 2D vector ` `// diagonally in decreasing order ` `#include "bits/stdc++.h" ` `using` `namespace` `std; ` ` ` `// Function that sort the elements ` `// of 2D vector ` `void` `diagonalSort(vector<vector<` `int` `> >& mat) ` `{ ` ` ` ` ` `// Calculate the rows and column ` ` ` `int` `row = mat.size(); ` ` ` `int` `col = mat[0].size(); ` ` ` ` ` `// Array of vectors to store the ` ` ` `// diagonal elements ` ` ` `vector<` `int` `> Neg[row]; ` ` ` `vector<` `int` `> Pos[col]; ` ` ` ` ` `// Traverse the 2D vector and put ` ` ` `// element in Array of vectors at ` ` ` `// index difference between indexes ` ` ` `for` `(` `int` `i = 0; i < row; i++) { ` ` ` `for` `(` `int` `j = 0; j < col; j++) { ` ` ` ` ` `// If diff is negative, then ` ` ` `// push element to Neg[] ` ` ` `if` `(j < i) { ` ` ` `Neg[i - j].push_back(mat[i][j]); ` ` ` `} ` ` ` ` ` `// If diff is positive, then ` ` ` `// push element to Pos[] ` ` ` `else` `if` `(j > i) { ` ` ` `Pos[j - i].push_back(mat[i][j]); ` ` ` `} ` ` ` ` ` `// If diff is 0, then push ` ` ` `// element to Pos[0] ` ` ` `else` `{ ` ` ` `Pos[0].push_back(mat[i][j]); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Sort the Array of vectors ` ` ` `for` `(` `int` `i = 0; i < row; i++) { ` ` ` `sort(Neg[i].begin(), Neg[i].end()); ` ` ` `} ` ` ` `for` `(` `int` `i = 0; i < col; i++) { ` ` ` `sort(Pos[i].begin(), Pos[i].end()); ` ` ` `} ` ` ` ` ` `// Update the value to arr[][] ` ` ` `// from the sorted Array of vectors ` ` ` `for` `(` `int` `i = 0; i < row; i++) { ` ` ` `for` `(` `int` `j = 0; j < col; j++) { ` ` ` ` ` `// If diff is positive ` ` ` `if` `(j < i) { ` ` ` `int` `d = i - j; ` ` ` `int` `l = Neg[d].size() - 1; ` ` ` `mat[i][j] = Neg[d][l - 1]; ` ` ` `Neg[d].pop_back(); ` ` ` `} ` ` ` ` ` `// If diff is negative ` ` ` `else` `if` `(j > i) { ` ` ` `int` `d = j - i; ` ` ` `int` `l = Pos[d].size() - 1; ` ` ` `mat[i][j] = Pos[d][l - 1]; ` ` ` `Pos[d].pop_back(); ` ` ` `} ` ` ` ` ` `// If diff is 0 ` ` ` `else` `{ ` ` ` `int` `l = Pos[0].size(); ` ` ` `mat[i][j] = Pos[0][l - 1]; ` ` ` `Pos[0].pop_back(); ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Function to print element ` `void` `printElement(vector<vector<` `int` `> >& arr) ` `{ ` ` ` ` ` `// Traverse the 2D vector ` ` ` `for` `(` `int` `i = 0; i < arr.size(); i++) { ` ` ` `for` `(` `int` `j = 0; j < arr[0].size(); j++) { ` ` ` `cout << arr[i][j] << ` `' '` `; ` ` ` `} ` ` ` `cout << endl; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `vector<vector<` `int` `> > arr = { { 10, 2, 3 }, ` ` ` `{ 4, 5, 6 }, ` ` ` `{ 7, 8, 9 } }; ` ` ` `diagonalSort(arr); ` ` ` ` ` `// Function call to print elements ` ` ` `printElement(arr); ` `} ` |

*chevron_right*

*filter_none*

**Output:**

10 2 0 4 9 0 0 0 5

**Time Complexity:** O(N^{2})

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.

## Recommended Posts:

- Sort a 2D vector diagonally using Map Data Structure
- Diagonally Dominant Matrix
- Print the matrix diagonally downwards
- Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
- Traverse the matrix in Diagonally Bottum-Up fashion using Recursion
- Comparison among Bubble Sort, Selection Sort and Insertion Sort
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Odd-Even Sort / Brick Sort
- Bucket Sort To Sort an Array with Negative Numbers
- Sort all even numbers in ascending order and then sort all odd numbers in descending order
- Serial Sort v/s Parallel Sort in Java
- Program to sort an array of strings using Selection Sort
- C/C++ Program for Odd-Even Sort / Brick Sort
- Java Program for Odd-Even Sort / Brick Sort
- Insertion sort to sort even and odd positioned elements in different orders
- Quick Sort vs Merge Sort
- Odd Even Transposition Sort / Brick Sort using pthreads
- Sort an Array which contain 1 to N values in O(N) using Cycle Sort

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.