Wednesday, July 3, 2024
HomeLanguagesPhpPhp Program to Rotate Matrix Elements

Php Program to Rotate Matrix Elements

Given a matrix, clockwise rotate elements in it.

Examples:

Input
1    2    3
4    5    6
7    8    9

Output:
4    1    2
7    5    3
8    9    6

For 4*4 matrix
Input:
1    2    3    4    
5    6    7    8
9    10   11   12
13   14   15   16

Output:
5    1    2    3
9    10   6    4
13   11   7    8
14   15   16   12

The idea is to use loops similar to the program for printing a matrix in spiral form. One by one rotate all rings of elements, starting from the outermost. To rotate a ring, we need to do following. 
    1) Move elements of top row. 
    2) Move elements of last column. 
    3) Move elements of bottom row. 
    4) Move elements of first column. 
Repeat above steps for inner ring while there is an inner ring.

Below is the implementation of above idea. Thanks to Gaurav Ahirwar for suggesting below solution. 

PHP




<?php
// PHP program to rotate a matrix
$R = 4;
$C = 4;
 
// A function to rotate a matrix
// mat[][] of size R x C. Initially,
// m = R and n = C
function rotatematrix($m, $n, $mat)
{
    global $R, $C;
    $row = 0;
    $col = 0;
    $prev = 0;
    $curr = 0;
 
    /*
    row - Starting row index
    m - ending row index
    col - starting column index
    n - ending column index
    i - iterator
    */
    while ($row < $m && $col < $n)
    {
 
        if ($row + 1 == $m ||
            $col + 1 == $n)
            break;
 
        // Store the first element
        // of next row, this element
        // will replace first element
        // of current row
        $prev = $mat[$row + 1][$col];
 
        /* Move elements of first row
           from the remaining rows */
        for ($i = $col; $i < $n; $i++)
        {
            $curr = $mat[$row][$i];
            $mat[$row][$i] = $prev;
            $prev = $curr;
        }
        $row++;
 
        /* Move elements of last column
           from the remaining columns */
        for ($i = $row; $i < $m; $i++)
        {
            $curr = $mat[$i][$n - 1];
            $mat[$i][$n - 1] = $prev;
            $prev = $curr;
        }
        $n--;
 
        /* Move elements of last row
           from the remaining rows */
        if ($row < $m)
        {
            for ($i = $n - 1;
                 $i >= $col; $i--)
            {
                $curr = $mat[$m - 1][$i];
                $mat[$m - 1][$i] = $prev;
                $prev = $curr;
            }
        }
        $m--;
 
        /* Move elements of first column
           from the remaining rows */
        if ($col < $n)
        {
            for ($i = $m - 1;
                 $i >= $row; $i--)
            {
                $curr = $mat[$i][$col];
                $mat[$i][$col] = $prev;
                $prev = $curr;
            }
        }
        $col++;
    }
 
    // Print rotated matrix
    for ($i = 0; $i < $R; $i++)
    {
        for ($j = 0; $j < $C; $j++)
        echo $mat[$i][$j] . " ";
        echo "
";
    }
}
 
// Driver code
 
// Test Case 1
$a = array(array(1, 2, 3, 4),
           array(5, 6, 7, 8),
           array(9, 10, 11, 12),
           array(13, 14, 15, 16));
 
// Test Case 2
/* int $a = array(array(1, 2, 3),
                  array(4, 5, 6),
                  array(7, 8, 9));
*/ rotatematrix($R, $C, $a);
    return 0;
     
// This code is contributed
// by ChitraNayal
?>


Output: 

5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12

Time Complexity: O(max(m,n) * max(m,n))
Auxiliary Space: O(m*n)

Please refer complete article on Rotate Matrix Elements for more details!
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments