PHP Program to Print a given matrix in reverse spiral form
Last Updated :
22 Jul, 2024
Improve
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 16
Output:
10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1
Input:
1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
Output:
11 10 9 8 7 13 14 15 16 17 18 12 6 5 4 3 2 1
Steps to implement Reverse Spiral Traversal of matrix:
- Define a 3x6 matrix and assign values to each element in the matrix.
- Initialize the starting indices for rows and columns, and prepare an array to store elements.
- Use a while loop to traverse the matrix in a spiral order, storing elements in an array.
- First row (left to right).
- Last column (top to bottom).
- Last row (right to left, if remaining).
- First column (bottom to top, if remaining).
- Update indices after each traversal.
- Print the stored elements from the array in reverse order, achieving the reverse spiral form.
Below is the implementation of the above approach:
<?php
// PHP Code for Print a given
// matrix in reverse spiral form
$R=3;
$C=6;
// Function that print matrix
// in reverse spiral form.
function ReversespiralPrint($m, $n, array $a)
{
// Large array to initialize it
// with elements of matrix
$b;
// k - starting row index
// l - starting column index
$k = 0;
$l = 0;
// Counter for single dimension array
// in which elements will be stored
$z = 0;
// Total elements in matrix
$size = $m*$n;
while ($k < $m && $l < $n)
{
// Variable to store
// value of matrix.
$val;
// Print the first row from
// the remaining rows
for ($i = $l; $i < $n; ++$i)
{
$val = $a[$k][$i];
$b[$z] = $val;
++$z;
}
$k++;
// Print the last column from
// the remaining columns
for ($i = $k; $i < $m; ++$i)
{
// printf("%d ", a[i][n-1]);
$val = $a[$i][$n-1];
$b[$z] = $val;
++$z;
}
$n--;
// Print the last row from
// the remaining rows
if ( $k < $m)
{
for ($i = $n-1; $i >= $l; --$i)
{
// printf("%d ", a[m-1][i]);
$val = $a[$m-1][$i];
$b[$z] = $val;
++$z;
}
$m--;
}
// Print the first column
// from the remaining columns
if ($l < $n)
{
for ($i = $m - 1; $i >= $k; --$i)
{
$val = $a[$i][$l];
$b[$z] = $val;
++$z;
}
$l++;
}
}
for ($i = $size - 1; $i >= 0; --$i)
{
echo $b[$i]." ";
}
}
// Driver Code
$a= array(array(1, 2, 3, 4, 5, 6),
array(7, 8, 9, 10, 11, 12),
array(13, 14, 15, 16, 17, 18));
ReversespiralPrint($R, $C, $a);
// This Code is contributed by mits
?>
<?php
// PHP Code for Print a given
// matrix in reverse spiral form
$R=3;
$C=6;
// Function that print matrix
// in reverse spiral form.
function ReversespiralPrint($m, $n, array $a)
{
// Large array to initialize it
// with elements of matrix
$b;
// k - starting row index
// l - starting column index
$k = 0;
$l = 0;
// Counter for single dimension array
// in which elements will be stored
$z = 0;
// Total elements in matrix
$size = $m*$n;
while ($k < $m && $l < $n)
{
// Variable to store
// value of matrix.
$val;
// Print the first row from
// the remaining rows
for ($i = $l; $i < $n; ++$i)
{
$val = $a[$k][$i];
$b[$z] = $val;
++$z;
}
$k++;
// Print the last column from
// the remaining columns
for ($i = $k; $i < $m; ++$i)
{
// printf("%d ", a[i][n-1]);
$val = $a[$i][$n-1];
$b[$z] = $val;
++$z;
}
$n--;
// Print the last row from
// the remaining rows
if ( $k < $m)
{
for ($i = $n-1; $i >= $l; --$i)
{
// printf("%d ", a[m-1][i]);
$val = $a[$m-1][$i];
$b[$z] = $val;
++$z;
}
$m--;
}
// Print the first column
// from the remaining columns
if ($l < $n)
{
for ($i = $m - 1; $i >= $k; --$i)
{
$val = $a[$i][$l];
$b[$z] = $val;
++$z;
}
$l++;
}
}
for ($i = $size - 1; $i >= 0; --$i)
{
echo $b[$i]." ";
}
}
// Driver Code
$a= array(array(1, 2, 3, 4, 5, 6),
array(7, 8, 9, 10, 11, 12),
array(13, 14, 15, 16, 17, 18));
ReversespiralPrint($R, $C, $a);
// This Code is contributed by mits
?>
Output
11 10 9 8 7 13 14 15 16 17 18 12 6 5 4 3 2 1
Complexity Analysis:
- Time complexity: O(m*n) where m is number of rows and n is number of columns of a given matrix
- Auxiliary space: O(1) as using constant variables
Please refer complete article on Print a given matrix in reverse spiral form for more details!.