-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrint elements in sorted order using row-column wise sorted matrix
57 lines (52 loc) · 1.46 KB
/
Print elements in sorted order using row-column wise sorted matrix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//{ Driver Code Starts
// Initial Template for Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String args[]) throws IOException {
BufferedReader read =
new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(read.readLine());
while (t-- > 0) {
int N = Integer.parseInt(read.readLine());
int v[][] = new int[N][N];
for (int i = 0; i < N; i++) {
String s[] = read.readLine().split(" ");
for (int j = 0; j < N; j++) v[i][j] = Integer.parseInt(s[j]);
}
Solution ob = new Solution();
int ans[][] = ob.sortedMatrix(N, v);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) System.out.print(ans[i][j] + " ");
System.out.println();
}
}
}
}
// } Driver Code Ends
// User function Template for Java
class Solution
{
int[][] sortedMatrix(int N, int Mat[][])
{
int arr[] = new int[N*N];
int k=0;
for(int i=0;i<Mat.length; i++)
{
for(int j=0; j<Mat[i].length; j++)
{
arr[k++]=Mat[i][j];
}
}
Arrays.sort(arr);
k=0;
for(int i=0;i<Mat.length; i++)
{
for(int j=0; j<Mat[i].length; j++)
{
Mat[i][j]=arr[k++];
}
}
return Mat;
}
};