Sorted matrix | Practice | GeeksforGeeks

Given an NxN matrix Mat. Sort all elements of the matrix.
Example 1:
Input:
N=4
Mat=[[10,20,30,40],
[15,25,35,45] 
[27,29,37,48] 
[32,33,39,50]]
Output:
10 15 20 25 
27 29 30 32
33 35 37 39
40 45 48 50
Explanation:
Sorting the matrix gives this result.
Example 2:
Input:
N=3
Mat=[[1,5,3],[2,8,7],[4,6,9]]
Output:
1 2 3 
4 5 6
7 8 9
Explanation:
Sorting the matrix gives this result.
Your Task:
You don't need to read input or print anything. Your task is to complete the function sortedMatrix() which takes the integer N and the matrix Mat as input parameters and returns the sorted matrix.

Expected Time Complexity:O(N2LogN)
Expected Auxillary Space:O(N2)

Constraints:
1<=N<=1000
1<=Mat[i][j]<=105
+1
bhardwajji6 days ago
vector<vector<int>> sortedMatrix(int n, vector<vector<int>> mat) {
        
        priority_queue<int , vector<int> , greater<int>>vec;
        
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++){
            vec.push(mat[i][j]);
        }
        
        
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++){
            mat[i][j] = vec.top();
            vec.pop();
        }
        
        return mat;
        
    }
+1
badgujarsachin832 weeks ago
  vector<vector<int>> sortedMatrix(int N, vector<vector<int>> Mat) {
        // code here
        vector<int> v;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                v.push_back(Mat[i][j]);
            }
        }
        int k=0;
        sort(v.begin(),v.end());
        vector<vector<int>> ans(N,vector<int>(N));
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                ans[i][j]=v[k];
                k++;
            }
        }
        return ans;
    }
0
aloksinghbais023 weeks ago
C++ solution having time complexity as O(N*N*log(N*N)) and space complexity as O(N*N) is as follows :- 
 
Execution Time :- 1.07 / 2.27 sec
 
vector<vector<int>> sortedMatrix(int N, vector<vector<int>> Mat) {       vector<int> v;       for(int i = 0; i < N; i++){           for(int j = 0; j < N; j++){               v.push_back(Mat[i][j]);           }       }              sort(v.begin(),v.end());       int ind = 0;       vector<vector<int>> ans(N,vector<int>(N));       for(int i = 0; i < N; i++){           for(int j = 0; j < N; j++){               ans[i][j] = v[ind++];           }       }              return (ans);   }
+1
arthurshelby4 weeks ago
😉🇮🇳✌️❄️
class Solution {
  public:
    vector<vector<int>> sortedMatrix(int N, vector<vector<int>> Mat) {
        // code here
        vector<int>v;
        for(int i=0;i<Mat.size();i++)
        {
            for(int j=0;j<Mat.size();j++)
            {
                v.push_back(Mat[i][j]);
            }
        }
        sort(v.begin(),v.end());
        int c=0;
        for(int i=0;i<Mat.size();i++)
        {
            for(int j=0;j<Mat.size();j++)
            {
                Mat[i][j]=v[c];
                c++;
            }
        }
        return Mat;
    }
};
0
gurucharanchouhan171 month ago
Solution in java :)
 
class Solution {
    int[][] sortedMatrix(int N, int a[][]) {
        
        ArrayList<Integer>l=new ArrayList<>();
        
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                l.add(a[i][j]);
            }
        }
        Collections.sort(l);
        
        int k=0;
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                a[i][j]=l.get(k++);
            }
        }
        return a;
    }
};
0
anubhavnegi541 month ago
Simple JAVA SOLUTION
 

class Solution {
    int[][] sortedMatrix(int N, int Mat[][]) {
        // code here
        N = Mat.length;
        int[] mat = new int[N * N];

        int k = 0;
        int z =0;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {

                mat[k++] = Mat[i][j];
            }
        }

        Arrays.sort(mat);

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {

                Mat[i][j] = mat[z++];
            }
        }
        return Mat;
    }
};
0
mishra07adi1 month ago
C++ Solution
class Solution {
  public:
    vector<vector<int>> sortedMatrix(int N, vector<vector<int>> Mat) {
        // code here
        vector<int> arr;
        
        for(int i=0;i<N;i++)
            for(int j=0;j<N;j++)
                arr.push_back(Mat[i][j]);
        
        sort(arr.begin(),arr.end());
        
        int k = 0;
        for(int i=0;i<N;i++)
            for(int j=0;j<N;j++)
                Mat[i][j] = arr[k++];
        
        return Mat;
        
    }
};
 
0
mishra07adi1 month ago
JAVA Solution
class Solution 
{
    int[][] sortedMatrix(int N, int Mat[][]) 
    {
        // code here
        int arr[] = new int[N*N];
        int k=0;
        for(int i=0;i<N;i++)
            for(int j=0;j<N;j++)
                arr[k++]=Mat[i][j];
        
        Arrays.sort(arr);
        
        k = 0;
        for(int i=0;i<N;i++)
            for(int j=0;j<N;j++)
                Mat[i][j] = arr[k++];
        
        return Mat;
        
    }
};
0
subhashishde081 month ago
class Solution {
  public:
    vector<vector<int>> sortedMatrix(int N, vector<vector<int>> Mat) {
        // code here
        vector<int>temp;
        int r = N;
        int c = Mat[0].size();
        for(int i=0;i<r;i++){
            for(int j=0;j<c;j++){
                temp.push_back(Mat[i][j]);
            }
        }
        sort(temp.begin(),temp.end());
        int j=0;
        int i=0;
        for(int x=0;x<temp.size();x++){
            if(j==c){
                j=0;
                i+=1;
            }
            Mat[i][j]=(temp[x]);
            j++;
        }
        
        return Mat;
    }
};
0
abhishekgokhe25041 month ago
JAVA Solution using ArrayList
int mt[][]=new int[N][N];
        ArrayList<Integer> a=new ArrayList<>();
        for(int i=0; i<N; i++){
            for(int j=0; j<N; j++){
                a.add(Mat[i][j]);
            }
        }
        Collections.sort(a);
        int k =0;
        for(int i=0; i<N; i++){
            for(int j=0; j<N; j++){
                mt[i][j]=a.get(k);
                k++;
            }
        }
        return mt;
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.