Skip to content

Conversation

@PrashamMehta-04
Copy link

@PrashamMehta-04 PrashamMehta-04 commented Oct 7, 2025

You are given an n x n square matrix of integers grid. Return the matrix such that: -> The diagonals in the bottom-left triangle (including the middle diagonal) are sorted in non-increasing order. -> The diagonals in the top-right triangle are sorted in non-decreasing order.

Intuition

The problem requires sorting matrix diagonals differently based on their position:

Bottom-left triangle (including the main diagonal) → sort in non-increasing order
Top-right triangle → sort in non-decreasing order

Each diagonal can be uniquely identified by its starting position: either from the first column (bottom-left) or from the first row (top-right). Once we know the starting point of a diagonal, we can extract all its elements, sort them according to the required order, and place them back.

By processing one diagonal at a time, we ensure that sorting is localized and does not interfere with other diagonals. This approach leverages the small constraint on 𝑛 ≤ 10 to keep sorting simple and efficient.

Approach

Identify diagonals:
Bottom-left diagonals start from the first column (rows from n-1 to 0).
Top-right diagonals start from the first row (columns from 1 to n-1).
Extract elements of each diagonal into a temporary array.
Sort the diagonal:
Bottom-left → descending order (non-increasing)
Top-right → ascending order (non-decreasing)
Place elements back into the matrix along the same diagonal path.
Repeat for all diagonals until the entire matrix is updated.
Return the updated matrix.

Code Solution (C++)

class Solution {
public:
    vector<vector<int>> sortMatrix(vector<vector<int>>& grid) {
        vector<vector<int>> ans;
        vector<int> t;
        int cnt = 1;
        int j=0;
        for(int i=grid.size()-1;i>=0;i--){
            int p=i;
            while(j < grid.size() && p < grid.size()){
                t.push_back(grid[p][j]);
                p++;
                j++;
            }
            sort(t.begin(), t.end());
            reverse(t.begin(), t.end());
            ans.push_back(t);
            t.clear();
            j=0;
        }
        vector<vector<int>> a1;
        int r = 1;
        for(int k=1;k<grid.size();k++){
            int q = 0;
            int d = r;
            while(q < grid.size() && d < grid.size()){
                t.push_back(grid[q][d]);
                q++;
                d++;
            }
            sort(t.begin(), t.end());
            a1.push_back(t);
            t.clear();
            r++;
        }
        for(int i=0;i<a1.size();i++){
            ans.push_back(a1[i]);
        }
        j = grid.size()-1;
        int p = 0,q=0;
        for(int i=grid.size()-1;i>=0;i--){
            int r = i;
            while(q < ans[p].size()){
                grid[r][j] = ans[p][q];
                q++;
                r++;
                j++;
            } 
            j=0;
            q=0;
            p++;
        }
        q=0;
        j=1;
        r=0;
        for(int i=1;i<grid.size();i++){
            while(q < ans[p].size()){
                grid[r][j] = ans[p][q];
                q++;
                r++;
                j++;
            }
            j=i+1;
            r=0;
            q=0;
            p++;
        }
        return grid;
    }
};

By submitting this PR, I confirm that:

  • This is my original work not totally AI genearted
  • I have tested the solution thoroughly on leetcode
  • I have maintained proper PR description format
  • This is a meaningful contribution, not spam

You are given an n x n square matrix of integers grid. Return the matrix such that:
-> The diagonals in the bottom-left triangle (including the middle diagonal) are sorted in non-increasing order.
-> The diagonals in the top-right triangle are sorted in non-decreasing order.

Approach:
We need to sort the diagonals of a square matrix in two different orders depending on their position:

Bottom-left triangle diagonals (including the main diagonal) → non-increasing order
Top-right triangle diagonals → non-decreasing order

The matrix can be divided into diagonals starting from the first column and diagonals starting from the first row (except the main diagonal). For each diagonal, we:
-> Extract all elements
-> Sort them in the required order (ascending for top-right, descending for bottom-left)
-> Place them back into their original positions

Intuition:
A diagonal in an n×n matrix is uniquely determined by its starting position either from the first column (for bottom-left diagonals) or from the first row (for top-right diagonals). By sorting each diagonal individually and placing it back, we can achieve the desired matrix arrangement efficiently.
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for raising the PR, the owner will be review it soon' keep patience, keep contributing>>>!!! make sure you have star ⭐ the repo

@SjxSubham

This comment was marked as resolved.

@SjxSubham SjxSubham changed the title Added the Leetcode problem number 3446 3446. Sort Matrix by Diagonals.cpp Oct 8, 2025
@SjxSubham SjxSubham linked an issue Oct 8, 2025 that may be closed by this pull request
4 tasks
@SjxSubham SjxSubham added the hacktoberest-accepted hacktoberfest-accepted label Oct 8, 2025
@SjxSubham

This comment was marked as duplicate.

@SjxSubham
Copy link
Owner

@PrashamMehta-04 Star the repo as well...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hacktoberest-accepted hacktoberfest-accepted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3446: Sort Matrix by Diagonals

2 participants