Sorting boundary elements of a matrix
Given a matrix mat[][] of size M*N, the task is to sort only the border elements of the matrix in the clockwise direction and print the matrix after sorting again.
Examples:
Input: M = 4, N = 5, Below is the given matrix:
1 2 3 4 0
1 1 1 1 2
1 2 2 2 4
1 9 3 1 7
Output:
0 1 1 1 1
9 1 1 1 1
7 2 2 2 2
4 4 3 3 2
Explanation:
For given matrix, border elements are:
(1, 2, 3, 4, 0, 2, 4, 7, 1, 3, 9, 1, 1, 1)
After sorting in clockwise order:
(0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 7, 9)
Input: M = 3, N = 4
4 2 8 0
2 6 9 8
0 3 1 7
Output:
0 0 1 2
8 6 9 2
8 7 4 3
Explanation:
For given matrix, border elements are:
(4, 2, 8, 0, 8, 7, 1, 3, 0, 2)
After sorting in clockwise order:
(0, 0, 1, 2, 2, 3, 4, 7, 8, 8)
Approach: The idea is to store all border elements of the given matrix in an array and sort this array then simply print the new matrix using this sorted array as the border elements.
Detailed steps are as follows:
- Traverse the given matrix and push all the boundary elements to an array A[].
- Sort array A[] in ascending order.
- Print first row using first N elements of array A[].
- From second row to second-last row, first print a single element from end of A[], then print N-2 middle elements from original matrix and finally a single element from the front of A[].
- For last row, print middle elements from A[] which are still not printed, in reverse order.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printMatrix( int grid[][5], int m, int n)
{
vector< int > A;
for ( int i = 0; i < m; i++)
{
for ( int j = 0; j < n; j++)
{
if (j == n - 1 || (i == m - 1) || j == 0
|| i == 0)
A.push_back(grid[i][j]);
}
}
sort(A.begin(), A.end());
for ( int i = 0; i < n; i++)
cout << A[i] << " " ;
cout << endl;
for ( int i = 0; i < m - 2; i++)
{
cout << A[A.size() - i - 1] << " " ;
for ( int j = 1; j < n - 1; j++)
cout << grid[i + 1][j] << " " ;
cout << (A[n + i]) << endl;
}
reverse(A.begin() + n + m - 2,
A.begin() + n + m + n - 2);
for ( int i = n + m - 2; i < n + m - 2 + n; i++)
cout << A[i] << " " ;
}
int main()
{
int m = 4, n = 5;
int grid[][5] = { { 1, 2, 3, 4, 0 },
{ 1, 1, 1, 1, 2 },
{ 1, 2, 2, 2, 4 },
{ 1, 9, 3, 1, 7 } };
printMatrix(grid, m, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void printMatrix( int [][] grid, int m, int n)
{
ArrayList<Integer> A = new ArrayList<>();
for ( int i = 0 ; i < m; i++)
{
for ( int j = 0 ; j < n; j++)
{
if (j == n - 1 || (i == m - 1 ) || j == 0
|| i == 0 )
A.add(grid[i][j]);
}
}
Collections.sort(A);
for ( int i = 0 ; i < n; i++)
System.out.print(A.get(i)+ " " );
System.out.print( '\n' );
for ( int i = 0 ; i < m - 2 ; i++)
{
System.out.print(A.get(A.size() - i - 1 )+ " " );
for ( int j = 1 ; j < n - 1 ; j++)
System.out.print(grid[i + 1 ][j]+ " " );
System.out.println(A.get(n + i));
}
Collections.reverse(A.subList( n + m - 2 , n + m - 2 + n ) ) ;
for ( int i = n + m - 2 ; i < n + m - 2 + n; i++)
System.out.print(A.get(i)+ " " );
}
public static void main(String[] args)
{
int m = 4 , n = 5 ;
int [][] grid = { { 1 , 2 , 3 , 4 , 0 },
{ 1 , 1 , 1 , 1 , 2 },
{ 1 , 2 , 2 , 2 , 4 },
{ 1 , 9 , 3 , 1 , 7 } };
printMatrix(grid, m, n);
}
}
|
Python3
def printMatrix(grid, m, n):
A = []
for i in range (m):
for j in range (n):
if j = = n - 1 or (i = = m - 1
) or j = = 0 or i = = 0 :
A.append(grid[i][j])
A.sort()
print ( * A[:n])
for i in range (m - 2 ):
print (A[ len (A) - i - 1 ],
end = " " )
for j in range ( 1 , n - 1 ):
print (grid[i + 1 ][j],
end = " " )
print (A[n + i])
print ( * reversed (A[n + m - 2 :n + m - 2 + n]))
m, n = 4 , 5
grid = [[ 1 , 2 , 3 , 4 , 0 ],
[ 1 , 1 , 1 , 1 , 2 ],
[ 1 , 2 , 2 , 2 , 4 ],
[ 1 , 9 , 3 , 1 , 7 ]]
printMatrix(grid, m, n)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
public static void printMatrix( int [][] grid, int m, int n)
{
ArrayList A = new ArrayList();
for ( int i = 0; i < m; i++)
{
for ( int j = 0; j < n; j++)
{
if (j == n - 1 || (i == m - 1) || j == 0 || i == 0)
{
A.Add(grid[i][j]);
}
}
}
A.Sort();
for ( int i = 0; i < n; i++)
{
Console.Write(A[i] + " " );
}
Console.Write( '\n' );
for ( int i = 0; i < m - 2; i++)
{
Console.Write(A[A.Count - i - 1] + " " );
for ( int j = 1; j < n - 1; j++)
{
Console.Write(grid[i + 1][j] + " " );
}
Console.WriteLine(A[n + i]);
}
A.Reverse(n + m - 2, n);
for ( int i = n + m - 2; i < n + m - 2 + n; i++)
{
Console.Write(A[i] + " " );
}
}
public static void Main( string [] args)
{
int m = 4, n = 5;
int [][] grid = new int [][]{
new int []{1,2,3,4,0},
new int []{1,1,1,1,2},
new int []{1,2,2,2,4},
new int []{1,9,3,1,7}
};
printMatrix(grid, m, n);
}
}
|
Javascript
<script>
function printMatrix(grid, m, n)
{
let A = [];
for (let i = 0; i < m; i++)
{
for (let j = 0; j < n; j++)
{
if (j == n - 1 || (i == m - 1) ||
j == 0 || i == 0)
A.push(grid[i][j]);
}
}
A.sort( function (a, b){ return a - b;});
for (let i = 0; i < n; i++)
document.write(A[i] + " " );
document.write( "<br>" )
for (let i = 0; i < m - 2; i++)
{
document.write(A[A.length - i - 1] + " " );
for (let j = 1; j < n - 1; j++)
document.write(grid[i + 1][j] + " " );
document.write(A[n + i] + "<br>" )
}
document.write(A.slice(
n + m - 2, n + m - 2 + n).reverse().join( " " ));
}
let m = 4, n = 5;
let grid = [ [ 1, 2, 3, 4, 0 ],
[ 1, 1, 1, 1, 2 ],
[ 1, 2, 2, 2, 4 ],
[ 1, 9, 3, 1, 7 ] ];
printMatrix(grid, m, n);
</script>
|
Output:
0 1 1 1 1
9 1 1 1 1
7 2 2 2 2
4 4 3 3 2
Time Complexity: O(M*N)
Auxiliary Space: O(M+N)
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...