Java Program to Rotate Matrix Elements
Matrix is simply a two-dimensional array. So the goal is to deal with fixed indices at which elements are present and to perform operations on indexes such that elements on the addressed should be swapped in such a manner it should lookout as the matrix is rotated. Here we will be discussing two methods in dealing with indices
- Using naive approach
- Using optimal approach
Method 1: Using naive approach
For a given matrix, the task is to rotate its elements in a clockwise direction.
Illustrations:
For 4*4 matrix
Input:
7 8 9
10 11 12
2 3 4
Output:
10 7 8
2 11 9
3 4 12
For 4*4 matrix
Input:
4 5 6 7
8 9 10 11
12 13 14 15
16 17 18 19
Output:
8 4 5 6
12 13 9 7
16 14 10 11
17 18 19 15
Approach:
Here, we will use loops in order to print the elements in spiral form. Where, we will rotate all the rings of the elements one by one, starting from the outermost one. And for rotating a ring, we need to do the following:
- Move the elements of the top row,
- Move the elements of the last column,
- Move the elements of the bottom row, and
- Move the elements of the first column.
Moreover, repeat the above steps if there is an inner ring as well.
Example:
Java
import java.lang.*;
import java.util.*;
class GFG {
static int r = 4 ;
static int c = 4 ;
static void rotate_matrix( int p, int q, int matrix[][])
{
int rw = 0 , cl = 0 ;
int previous, current;
while (rw < p && cl < q) {
if (rw + 1 == p || cl + 1 == q)
break ;
previous = matrix[rw + 1 ][cl];
for ( int x = cl; x < q; x++) {
current = matrix[rw][x];
matrix[rw][x] = previous;
previous = current;
}
rw++;
for ( int x = rw; x < p; x++) {
current = matrix[x][q - 1 ];
matrix[x][q - 1 ] = previous;
previous = current;
}
q--;
if (rw < p) {
for ( int x = q - 1 ; x >= cl; x--) {
current = matrix[p - 1 ][x];
matrix[p - 1 ][x] = previous;
previous = current;
}
}
p--;
if (cl < q) {
for ( int x = p - 1 ; x >= rw; x--) {
current = matrix[x][cl];
matrix[x][cl] = previous;
previous = current;
}
}
cl++;
}
for ( int x = 0 ; x < r; x++) {
for ( int y = 0 ; y < c; y++)
System.out.print(matrix[x][y] + " " );
System.out.print( "\n" );
}
}
public static void main(String[] args)
{
int b[][] = { { 5 , 6 , 7 , 8 },
{ 1 , 2 , 3 , 4 },
{ 0 , 15 , 6 , 5 },
{ 3 , 1 , 2 , 12 } };
rotate_matrix(r, c, b);
}
}
|
Output
1 5 6 7
0 15 2 8
3 6 3 4
1 2 12 5
Method 2: Using optimal approach
For a given matrix of size M×N, we need to rotate the matrix elements by k times to the right side. Where k is a number.
Approach:
An optimal approach is to observe each row of the stated matrix as an array and then execute an array rotation. It is performed by replicating the elements of the matrix from the given number k to the end of an array to the starting of the array utilizing a temporary array. And then the rest of the elements from the start to (k-1) to the end of an array.
Illustration:
Input : M = 3, N = 3, k = 2
1 2 3
4 5 6
7 8 9
Output : 2 3 1
5 6 4
8 9 7
Input : M = 2, N = 2, k = 2
11 12
13 14
Output : 11 12
13 14
Example:
Java
public class GFG {
static final int P = 3 ;
static final int Q = 3 ;
static void rotate_Matrix( int mat[][], int K)
{
int tempo[] = new int [P];
K = K % P;
for ( int j = 0 ; j < Q; j++) {
for ( int l = 0 ; l < P - K; l++)
tempo[l] = mat[j][l];
for ( int x = P - K; x < P; x++)
mat[j][x - P + K] = mat[j][x];
for ( int x = K; x < P; x++)
mat[j][x] = tempo[x - K];
}
}
static void show_Matrix( int mat[][])
{
for ( int j = 0 ; j < Q; j++) {
for ( int x = 0 ; x < P; x++)
System.out.print(mat[j][x] + " " );
System.out.println();
}
}
public static void main(String[] args)
{
int mat[][]
= { { 1 , 2 , 5 }, { 3 , 4 , 6 }, { 8 , 10 , 9 } };
int K = 2 ;
rotate_Matrix(mat, K);
show_Matrix(mat);
}
}
|
Output
2 5 1
4 6 3
10 9 8
Time complexity: O(R*C) where R and C are no of rows and columns of given matrix respectively
Auxiliary space: O(1) because using array tempo of size 3
Last Updated :
05 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...