PHP Program to Inplace rotate square matrix by 90 degrees | Set 1
Last Updated :
23 Jul, 2024
Given a square matrix, turn it by 90 degrees in an anti-clockwise direction without using any extra space.
Examples :
Input:
Matrix:
1 2 3
4 5 6
7 8 9
Output:
3 6 9
2 5 8
1 4 7
The given matrix is rotated by 90 degree
in anti-clockwise direction.
Input:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Output:
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
The given matrix is rotated by 90 degree
in anti-clockwise direction.
Approach:
To solve the question without any extra space, rotate the array in the ,form of squares, dividing the matrix into squares or cycles. For example,
A 4 X 4 matrix will have 2 cycles. The first cycle is formed by its 1st row, last column, last row, and 1st column. The second cycle is formed by 2nd row, second-last column, second-last row, and 2nd column. The idea is for each square cycle, to swap the elements involved with the corresponding cell in the matrix in anti-clockwise direction i.e. from top to left, left to bottom, bottom to right, and from right to top one at a time using nothing but a temporary variable to achieve this.
Demonstration:
First Cycle (Involves Red Elements)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Moving first group of four elements (First
elements of 1st row, last row, 1st column
and last column) of first cycle in counter
clockwise.
4 2 3 16
5 6 7 8
9 10 11 12
1 14 15 13
Moving next group of four elements of
first cycle in counter clockwise
4 8 3 16
5 6 7 15
2 10 11 12
1 14 9 13
Moving final group of four elements of
first cycle in counter clockwise
4 8 12 16
3 6 7 15
2 10 11 14
1 5 9 13
Second Cycle (Involves Blue Elements)
4 8 12 16
3 6 7 15
2 10 11 14
1 5 9 13
Fixing second cycle
4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
Algorithm:
- There is N/2 squares or cycles in a matrix of side N. Process a square one at a time. Run a loop to traverse the matrix a cycle at a time, i.e loop from 0 to N/2 - 1, loop counter is i
- Consider elements in group of 4 in current square, rotate the 4 elements at a time. So the number of such groups in a cycle is N - 2*i.
- So run a loop in each cycle from x to N - x - 1, loop counter is y
- The elements in the current group is (x, y), (y, N-1-x), (N-1-x, N-1-y), (N-1-y, x), now rotate the these 4 elements, i.e (x, y) <- (y, N-1-x), (y, N-1-x)<- (N-1-x, N-1-y), (N-1-x, N-1-y)<- (N-1-y, x), (N-1-y, x)<- (x, y)
- Print the matrix.
Below is the implementation of the above approach:
PHP
<?php
// PHP program to rotate a
// matrix by 90 degrees
$N = 4;
// An Inplace function to
// rotate a N x N matrix
// by 90 degrees in
// anti-clockwise direction
function rotateMatrix(&$mat)
{
global $N;
// Consider all
// squares one by one
for ($x = 0; $x < $N / 2; $x++)
{
// Consider elements
// in group of 4 in
// current square
for ($y = $x;
$y < $N - $x - 1; $y++)
{
// store current cell
// in temp variable
$temp = $mat[$x][$y];
// move values from
// right to top
$mat[$x][$y] = $mat[$y][$N - 1 - $x];
// move values from
// bottom to right
$mat[$y][$N - 1 - $x] =
$mat[$N - 1 - $x][$N - 1 - $y];
// move values from
// left to bottom
$mat[$N - 1 - $x][$N - 1 - $y] =
$mat[$N - 1 - $y][$x];
// assign temp to left
$mat[$N - 1 - $y][$x] = $temp;
}
}
}
// Function to
// print the matrix
function displayMatrix(&$mat)
{
global $N;
for ($i = 0; $i < $N; $i++)
{
for ($j = 0; $j < $N; $j++)
echo $mat[$i][$j] . " ";
echo "
";
}
echo "
";
}
// Driver code
// Test Case 1
$mat = array(array(1, 2, 3, 4),
array(5, 6, 7, 8),
array(9, 10, 11, 12),
array(13, 14, 15, 16));
// Tese Case 2
/* $mat = array(array(1, 2, 3),
array(4, 5, 6),
array(7, 8, 9));
*/
// Tese Case 3
/*$mat = array(array(1, 2),
array(4, 5));*/
// displayMatrix($mat);
rotateMatrix($mat);
// Print rotated matrix
displayMatrix($mat);
// This code is contributed
// by ChitraNayal
?>
Output4 8 12 16
3 7 11 15
2 6 10 14
1 5 9 13
Complexity Analysis:
- Time Complexity: O(n*n), where n is size of array.
A single traversal of the matrix is needed. - Space Complexity: O(1).
As a constant space is needed
Please refer complete article on Inplace rotate square matrix by 90 degrees | Set 1 for more details!
Similar Reads
PHP Program to Rotate the matrix right by K times Given a matrix of size N*M and a number K. We have to rotate the matrix K times to the right side. Examples: Input : N = 3, M = 3, K = 2 12 23 34 45 56 67 78 89 91 Output : 23 34 12 56 67 45 89 91 78 Input : N = 2, M = 2, K = 2 1 2 3 4 Output : 1 2 3 4A simple yet effective approach is to consider e
2 min read
PHP Program to Print a given matrix in reverse spiral form Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. Example: Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16Output: 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1Input: 1 2 3 4 5 6 7 8 9 10 11
3 min read
PHP Program to Find Sum of All Matrix Elements Finding the sum of all elements in a matrix is a common operation in mathematical computations and programming. In PHP, this can be achieved using various approaches, including loops and array functions. In this article, we will explore different methods to calculate the sum of all elements in a mat
4 min read
PHP Program to Sort the matrix row-wise and column-wise Given a n x n matrix. The problem is to sort the matrix row-wise and column-wise.Examples: Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} }Output : 1 3 4 2 5 7 6 8 9Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13 2
3 min read
PHP Program to Check if all rows of a matrix are circular rotations of each other Given a matrix of n*n size, the task is to find whether all rows are circular rotations of each other or not. Examples: Input: mat[][] = 1, 2, 3 3, 1, 2 2, 3, 1Output: Yes ,All rows are rotated permutation of each other.Input: mat[3][3] = 1, 2, 3 3, 2, 1 1, 3, 2Output: No, Explanation : As 3, 2, 1 i
2 min read