Posted on by Kalkicode
Code Matrix

# Rotate each ring of the matrix in anticlockwise form

The problem discussed in this code is to rotate each ring (or layer) of a given matrix in an anticlockwise direction by a specified number of elements. The matrix rings are the concentric layers that are formed while moving from the outermost layer towards the center of the matrix.

## Problem Statement

Given a matrix and an integer value `K`, we need to rotate each ring of the matrix by `K` elements in an anticlockwise direction. A ring consists of elements at a certain distance from the center of the matrix, and each ring should be rotated independently.

## Example

Consider the following matrix:

``````1   2   3   4   5
6   7   8   9  10
11  12  13  14  15
16  17  18  19  20``````

If we rotate each ring in the matrix by 3 elements in an anticlockwise direction, the result would be:

``````4   5  10  15  20
3  14  13  12  19
2   9   8   7  18
1   6  11  16  17``````

## Solution Idea

To solve this problem, we can iteratively rotate each ring of the matrix. We start from the outermost ring and work our way towards the center, rotating each ring by `K` elements in an anticlockwise direction.

## Pseudocode

``````change(matrix, row, col, index):
data = matrix[row][col]
temp = 0

# Rotate bottom right to top right
for i from row-1 down to index:
temp = matrix[i][col]
matrix[i][col] = data
data = temp

# Rotate top right to top left
for i from col-1 down to index:
temp = matrix[index][i]
matrix[index][i] = data
data = temp

# Rotate top left to bottom left
for i from index+1 up to row:
temp = matrix[i][index]
matrix[i][index] = data
data = temp

# Rotate bottom left to bottom right
for i from index+1 up to col:
temp = matrix[row][i]
matrix[row][i] = data
data = temp

rotate(matrix, size):
row = ROW, col = COL, occurrence = COL
if ROW < COL:
occurrence = ROW
for i from 0 to occurrence/2:
row--, col--
change(matrix, row, col, i)
if size > 1:
rotate(matrix, size-1)``````

## Algorithm Explanation

1. The `change` function is responsible for rotating a single ring by changing its elements. It takes the matrix, the row and column positions, and the index as parameters.
2. The `rotate` function is the main function responsible for rotating each ring. It takes the matrix and the size as parameters, where size is the number of elements to rotate by.
3. Start from the outermost ring and work inwards, rotating each ring.
4. Within the `change` function, rotate each side of the ring by the specified number of elements.
5. After rotating all sides, recursively call the `rotate` function for the inner rings if the size is greater than 1.

## Code Solution

``````//C Program
//Rotate each ring of matrix anticlockwise by K elements
#include<stdio.h>

#define ROW 4
#define COL 5

void change(int matrix[][COL],int row,int col,int index)
{
int data = matrix[row][col],temp=0;

//bottom right to top rigth
for (int i = row-1; i >= index ; --i)
{
temp=matrix[i][col];
matrix[i][col]=data;
data=temp;
}
//top right to top left
for (int i = col-1; i >=index ; --i)
{
temp=matrix[index][i];
matrix[index][i]=data;
data=temp;
}
//top left to bottom left
for (int i = index+1; i <=row ; ++i)
{
temp=matrix[i][index];
matrix[i][index]=data;
data=temp;
}
//bottom left to bottom right
for (int i = index+1; i <=col ; ++i)
{
temp=matrix[row][i];
matrix[row][i]=data;
data=temp;
}

}
void rotate(int matrix[][COL],int size)
{
int row=ROW, col=COL,occurrence=COL;

if(ROW < COL)
{
//When not a perfect square matrix
//Get small size
occurrence=ROW;
}
//This is rotating each ring by one size outer to inner side
/*
//outer ring
← ← ← ← ← ←
↓             ↑
↓   same as   ↑
↓             ↑
↓  inner ring ↑
↓             ↑
↓             ↑
→ → → → → →
*/
for (int i = 0; i < occurrence/2; ++i)
{
row--;
col--;
change(matrix,row,col,i);
}
if(size>1)
{
rotate(matrix,size-1);
}
}
//Display matrix element
void show_data(int matrix[][COL])
{
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
printf("%3d",matrix[i][j] );
}
printf("\n");
}
printf("\n");
}
int main()
{

int matrix[][COL]={
{1,  2,  3,  4 , 5},
{6,  7 , 8,  9, 10},
{11, 12, 13, 14, 15},
{ 16, 17, 18, 19, 20}
};

int size=3;
printf("Before\n");
show_data(matrix);
rotate(matrix,size);
printf("After rotate ring in %d times\n",size);

show_data(matrix);
return 0;
}

```
```

#### Output

``````Before
1  2  3  4  5
6  7  8  9 10
11 12 13 14 15
16 17 18 19 20

After rotate ring in 3 times
4  5 10 15 20
3 14 13 12 19
2  9  8  7 18
1  6 11 16 17
``````
``````#include<iostream>
#define ROW 4
#define COL 5
using namespace std;

/*
C++ Program
Rotate each ring of matrix anticlockwise by K elements
*/
class MyMatrix {
public:
int rows;
int cols;
MyMatrix() {
//Get the size of matrix
this->rows = ROW;
this->cols = COL;
}
void change(int matrix[][COL], int row, int col, int index) {
int data = matrix[row][col];
int temp = 0;
//bottom right to top rigth

for (int i = row - 1; i >= index; --i) {
temp = matrix[i][col];
matrix[i][col] = data;
data = temp;
}
//top right to top left

for (int i = col - 1; i >= index; --i) {
temp = matrix[index][i];
matrix[index][i] = data;
data = temp;
}
//top left to bottom left

for (int i = index + 1; i <= row; ++i) {
temp = matrix[i][index];
matrix[i][index] = data;
data = temp;
}
//bottom left to bottom right

for (int i = index + 1; i <= col; ++i) {
temp = matrix[row][i];
matrix[row][i] = data;
data = temp;
}
}
void rotate(int matrix[][COL], int size) {
int row = this->rows, col = this->cols, occurrence = this->cols;
if (row < col) {
//When not a perfect square matrix
//Get small size
occurrence = row;
}
//This is rotating each ring by one size outer to inner side
/*
//outer ring
← ← ← ← ← ←
↓             ↑
↓   same as   ↑
↓             ↑
↓  inner ring ↑
↓             ↑
↓             ↑
→ → → → → →
*/

for (int i = 0; i < occurrence / 2; ++i) {
row--;
col--;
this->change(matrix, row, col, i);
}
if (size > 1) {
this->rotate(matrix, size - 1);
}
}
//Display matrix elements
void show_data(int matrix[][COL]) {
for (int i = 0; i < this->rows; ++i) {
for (int j = 0; j < this->cols; ++j) {
cout << " " << matrix[i][j];
}
cout << "\n";
}
cout << "\n";
}
};
int main() {
int matrix[][COL] = {
{
1,
2,
3,
4,
5
},
{
6,
7,
8,
9,
10
},
{
11,
12,
13,
14,
15
},
{
16,
17,
18,
19,
20
}
};
MyMatrix obj;
int size = 3;
cout << "Before\n";
obj.show_data(matrix);
obj.rotate(matrix, size);
cout << "After rotate ring in " << size << " times\n";
obj.show_data(matrix);
return 0;
}```
```

#### Output

``````Before
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20

After rotate ring in 3 times
4 5 10 15 20
3 14 13 12 19
2 9 8 7 18
1 6 11 16 17
``````
``````/*
Java Program
Rotate each ring of matrix anticlockwise by K elements
*/
public class MyMatrix {

public int rows;
public int cols;
public MyMatrix(int [][]matrix)
{
//Get the size of matrix
rows = matrix.length;
cols = matrix[0].length;
}

public void change(int [][]matrix,int row,int col,int index)
{
int data = matrix[row][col];
int temp=0;

//bottom right to top rigth
for (int i = row-1; i >= index ; --i)
{
temp=matrix[i][col];
matrix[i][col]=data;
data=temp;
}
//top right to top left
for (int i = col-1; i >=index ; --i)
{
temp=matrix[index][i];
matrix[index][i]=data;
data=temp;
}
//top left to bottom left
for (int i = index+1; i <=row ; ++i)
{
temp=matrix[i][index];
matrix[i][index]=data;
data=temp;
}
//bottom left to bottom right
for (int i = index+1; i <=col ; ++i)
{
temp=matrix[row][i];
matrix[row][i]=data;
data=temp;
}

}
void rotate(int [][]matrix,int size)
{
int row=rows, col=cols,occurrence=cols;

if(row < col)
{
//When not a perfect square matrix
//Get small size
occurrence=row;
}
//This is rotating each ring by one size outer to inner side
/*
//outer ring
← ← ← ← ← ←
↓             ↑
↓   same as   ↑
↓             ↑
↓  inner ring ↑
↓             ↑
↓             ↑
→ → → → → →
*/
for (int i = 0; i < occurrence/2; ++i)
{
row--;
col--;
change(matrix,row,col,i);
}
if(size>1)
{
rotate(matrix,size-1);
}
}
//Display matrix elements
public void show_data(int [][]matrix)
{
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
System.out.print("  "+matrix[i][j] );
}
System.out.print("\n");
}
System.out.print("\n");
}

public static void main(String[] args) {

int [][]matrix={
{1,  2,  3,  4 , 5},
{6,  7 , 8,  9, 10},
{11, 12, 13, 14, 15},
{ 16, 17, 18, 19, 20}
};

MyMatrix obj = new MyMatrix(matrix);
int size=3;
System.out.print("Before\n");
obj.show_data(matrix);
obj.rotate(matrix,size);
System.out.print("After rotate ring in "+size+" times\n");

obj.show_data(matrix);

}
}```
```

#### Output

``````Before
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20

After rotate ring in 3 times
4 5 10 15 20
3 14 13 12 19
2 9 8 7 18
1 6 11 16 17
``````
``````using System;
/*
C# Program
Rotate each ring of matrix anticlockwise by K elements
*/

public class MyMatrix {
int rows;
int cols;
MyMatrix(int[,] matrix) {
//Get the size of matrix
rows = matrix.GetLength(0);
cols = matrix.GetLength(1);
}
public void change(int[,] matrix, int row, int col, int index) {
int data = matrix[row,col];
int temp = 0;
//bottom right to top rigth

for (int i = row - 1; i >= index; --i) {
temp = matrix[i,col];
matrix[i,col] = data;
data = temp;
}
//top right to top left

for (int i = col - 1; i >= index; --i) {
temp = matrix[index,i];
matrix[index,i] = data;
data = temp;
}
//top left to bottom left

for (int i = index + 1; i <= row; ++i) {
temp = matrix[i,index];
matrix[i,index] = data;
data = temp;
}
//bottom left to bottom right

for (int i = index + 1; i <= col; ++i) {
temp = matrix[row,i];
matrix[row,i] = data;
data = temp;
}
}
void rotate(int[,] matrix, int size) {
int row = rows, col = cols, occurrence = cols;
if (row < col) {
//Get small size
//When not a perfect square matrix
occurrence = row;
}

//This is rotating each ring by one size outer to inner side
/*
//outer ring
← ← ← ← ← ←
↓             ↑
↓   same as   ↑
↓             ↑
↓  inner ring ↑
↓             ↑
↓             ↑
→ → → → → →
*/

for (int i = 0; i < occurrence / 2; ++i) {
row--;
col--;
change(matrix, row, col, i);
}
if (size > 1) {
rotate(matrix, size - 1);
}
}
//Display matrix elements
public void show_data(int[,] matrix) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
Console.Write(" " + matrix[i,j]);
}
Console.Write("\n");
}
Console.Write("\n");
}
public static void Main(String[] args) {
int[,] matrix = {
{
1,
2,
3,
4,
5
},
{
6,
7,
8,
9,
10
},
{
11,
12,
13,
14,
15
},
{
16,
17,
18,
19,
20
}
};
MyMatrix obj = new MyMatrix(matrix);
int size = 3;
Console.Write("Before\n");
obj.show_data(matrix);
obj.rotate(matrix, size);
Console.Write("After rotate ring in " + size + " times\n");
obj.show_data(matrix);
}
}```
```

#### Output

``````Before
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20

After rotate ring in 3 times
4 5 10 15 20
3 14 13 12 19
2 9 8 7 18
1 6 11 16 17
``````
``````/*
Node Js Program
Rotate each ring of matrix anticlockwise by K elements
*/
class MyMatrix {
;;
constructor(matrix) {
//Get the size of matrix
this.rows = matrix.length;
this.cols = matrix[0].length;
}
change(matrix, row, col, index) {
var data = matrix[row][col];
var temp = 0;
//bottom right to top rigth

for (var i = row - 1; i >= index; --i) {
temp = matrix[i][col];
matrix[i][col] = data;
data = temp;
}

//top right to top left

for (var i = col - 1; i >= index; --i) {
temp = matrix[index][i];
matrix[index][i] = data;
data = temp;
}

//top left to bottom left

for (var i = index + 1; i <= row; ++i) {
temp = matrix[i][index];
matrix[i][index] = data;
data = temp;
}

//bottom left to bottom right

for (var i = index + 1; i <= col; ++i) {
temp = matrix[row][i];
matrix[row][i] = data;
data = temp;
}
}
rotate(matrix, size) {
var row = this.rows;
var col = this.cols;
var occurrence = this.cols;
if (row < col) {
//When not a perfect square matrix
//Get small size
occurrence = row;
}

//This is rotating each ring by one size outer to inner side
/*
//outer ring
← ← ← ← ← ←
↓             ↑
↓   same as   ↑
↓             ↑
↓  inner ring ↑
↓             ↑
↓             ↑
→ → → → → →
*/

for (var i = 0; i < parseInt(occurrence / 2); ++i) {
row--;
col--;
this.change(matrix, row, col, i);
}

if (size > 1) {
this.rotate(matrix, size - 1);
}
}

//Display matrix elements
show_data(matrix) {
for (var i = 0; i < this.rows; ++i) {
for (var j = 0; j < this.cols; ++j) {
process.stdout.write(" " + matrix[i][j]);
}

process.stdout.write("\n");
}

process.stdout.write("\n");
}
}

function main(args) {
var matrix = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]
];
var obj = new MyMatrix(matrix);
var size = 3;
process.stdout.write("Before\n");
obj.show_data(matrix);
obj.rotate(matrix, size);
process.stdout.write("After rotate ring in " + size + " times\n");
obj.show_data(matrix);
}
main();```
```

#### Output

``````Before
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20

After rotate ring in 3 times
4 5 10 15 20
3 14 13 12 19
2 9 8 7 18
1 6 11 16 17
``````
``````# Python 3 Program
# Rotate each ring of matrix anticlockwise by K elements
class MyMatrix :

def __init__(self, matrix) :
# Get the size of matrix
self.rows = len(matrix)
self.cols = len(matrix[0])

def change(self, matrix, row, col, index) :
data = matrix[row][col]
temp = 0
# bottom right to top rigth
i = row - 1
while (i >= index) :
temp = matrix[i][col]
matrix[i][col] = data
data = temp
i -= 1

# top right to top left
i = col - 1
while (i >= index) :
temp = matrix[index][i]
matrix[index][i] = data
data = temp
i -= 1

# top left to bottom left
i = index + 1
while (i <= row) :
temp = matrix[i][index]
matrix[i][index] = data
data = temp
i += 1

# bottom left to bottom right
i = index + 1
while (i <= col) :
temp = matrix[row][i]
matrix[row][i] = data
data = temp
i += 1

def rotate(self, matrix, size) :
row = self.rows
col = self.cols
occurrence = self.cols
if (row < col) :
# Get small size
# When not a perfect square matrix
# Get small size
occurrence = row

# This is rotating each ring by one size outer to inner side

# 		      //outer ring
# 		        ← ← ← ← ← ←
# 		      ↓             ↑
# 		      ↓   same as   ↑
# 		      ↓             ↑
# 		      ↓  inner ring ↑
# 		      ↓             ↑
# 		      ↓             ↑
# 		        → → → → → →
#
i = 0
while (i < int(occurrence / 2)) :
row -= 1
col -= 1
self.change(matrix, row, col, i)
i += 1

if (size > 1) :
self.rotate(matrix, size - 1)

# Display matrix elements
def show_data(self, matrix) :
i = 0
while (i < self.rows) :
j = 0
while (j < self.cols) :
print(" ", matrix[i][j], end = "")
j += 1

print("\n", end = "")
i += 1

print("\n", end = "")

def main() :
matrix = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]
]
obj = MyMatrix(matrix)
size = 3
print("Before\n", end = "")
obj.show_data(matrix)
obj.rotate(matrix, size)
print("After rotate ring in ", size ," times\n", end = "")
obj.show_data(matrix)

if __name__ == "__main__":
main()```
```

#### Output

``````Before
1  2  3  4  5
6  7  8  9  10
11  12  13  14  15
16  17  18  19  20

After rotate ring in  3  times
4  5  10  15  20
3  14  13  12  19
2  9  8  7  18
1  6  11  16  17
``````
``````# Ruby Program
# Rotate each ring of matrix anticlockwise by K elements
class MyMatrix
# Define the accessor and reader of class MyMatrix
attr_accessor :rows, :cols
def initialize(matrix)
# Get the size of matrix
@rows = matrix.length
@cols = matrix[0].length
end
def change(matrix, row, col, index)
data = matrix[row][col]
temp = 0
# bottom right to top rigth
i = row - 1
while (i >= index)
temp = matrix[i][col]
matrix[i][col] = data
data = temp
i -= 1
end
# top right to top left
i = col - 1
while (i >= index)
temp = matrix[index][i]
matrix[index][i] = data
data = temp
i -= 1
end
# top left to bottom left
i = index + 1
while (i <= row)
temp = matrix[i][index]
matrix[i][index] = data
data = temp
i += 1
end
# bottom left to bottom right
i = index + 1
while (i <= col)
temp = matrix[row][i]
matrix[row][i] = data
data = temp
i += 1
end
end
def rotate(matrix, size)
row = @rows
col = @cols
occurrence = @cols
if (row < col)
# Get small size
# When not a perfect square matrix
# Get small size
occurrence = row
end
# This is rotating each ring by one size outer to inner side

# 		      //outer ring
# 		        ← ← ← ← ← ←
# 		      ↓             ↑
# 		      ↓   same as   ↑
# 		      ↓             ↑
# 		      ↓  inner ring ↑
# 		      ↓             ↑
# 		      ↓             ↑
# 		        → → → → → →
#
i = 0
while (i < occurrence / 2)
row -= 1
col -= 1
self.change(matrix, row, col, i)
i += 1
end
if (size > 1)
self.rotate(matrix, size - 1)
end
end
# Display matrix elements
def show_data(matrix)
i = 0
while (i < @rows)
j = 0
while (j < @cols)
print(" ", matrix[i][j])
j += 1
end
print("\n")
i += 1
end
print("\n")
end
end
def main()
matrix = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]
]
obj = MyMatrix.new(matrix)
size = 3
print("Before\n")
obj.show_data(matrix)
obj.rotate(matrix, size)
print("After rotate ring in ", size ," times\n")
obj.show_data(matrix)
end

main()```
```

#### Output

``````Before
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20

After rotate ring in 3 times
4 5 10 15 20
3 14 13 12 19
2 9 8 7 18
1 6 11 16 17

``````
``````/*
Scala Program
Rotate each ring of matrix anticlockwise by K elements
*/
class MyMatrix(var rows: Int,var cols: Int) {

def this(matrix: Array[Array[Int]]) {
//Get the size of matrix
this(matrix.length,matrix(0).length);
}
def change(matrix: Array[Array[Int]], row: Int, col: Int, index: Int): Unit = {
var data: Int = matrix(row)(col);
var temp: Int = 0;

//bottom right to top rigth
var i: Int = row - 1;
while (i >= index) {
temp = matrix(i)(col);
matrix(i)(col) = data;
data = temp;
i -= 1;
}
//top right to top left
i = col - 1;
while (i >= index) {
temp = matrix(index)(i);
matrix(index)(i) = data;
data = temp;
i -= 1;
}
//top left to bottom left
i = index + 1;
while (i <= row) {
temp = matrix(i)(index);
matrix(i)(index) = data;
data = temp;
i += 1;
}
//bottom left to bottom right
i = index + 1;
while (i <= col) {
temp = matrix(row)(i);
matrix(row)(i) = data;
data = temp;
i += 1;
}
}
def rotate(matrix: Array[Array[Int]], size: Int): Unit = {
var row: Int = this.rows;
var col: Int = this.cols;
var occurrence: Int = this.cols;

if (row < col) {
//Get small size
//When not a perfect square matrix
//Get small size
occurrence = row;
}
//This is rotating each ring by one size outer to inner side
/*
//outer ring
← ← ← ← ← ←
↓             ↑
↓   same as   ↑
↓             ↑
↓  inner ring ↑
↓             ↑
↓             ↑
→ → → → → →
*/
var i: Int = 0;
while (i < (occurrence / 2).toInt) {
row -= 1;
col -= 1;
this.change(matrix, row, col, i);
i += 1;
}
if (size > 1) {
this.rotate(matrix, size - 1);
}
}
//Display matrix elements
def show_data(matrix: Array[Array[Int]]): Unit = {
var i: Int = 0;
while (i < this.rows) {
var j: Int = 0;
while (j < this.cols) {
print(" " + matrix(i)(j));
j += 1;
}
print("\n");
i += 1;
}
print("\n");
}
}
object Main {
def main(args: Array[String]): Unit = {
val matrix: Array[Array[Int]] = Array(
Array(1, 2, 3, 4, 5),
Array(6, 7, 8, 9, 10),
Array(11, 12, 13, 14, 15),
Array(16, 17, 18, 19, 20));
val obj: MyMatrix = new MyMatrix(matrix);
val size: Int = 3;
print("Before\n");
obj.show_data(matrix);
obj.rotate(matrix, size);
print("After rotate ring in " + size + " times\n");
obj.show_data(matrix);
}
}```
```

#### Output

``````Before
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20

After rotate ring in 3 times
4 5 10 15 20
3 14 13 12 19
2 9 8 7 18
1 6 11 16 17
``````
``````/*
Swift 4 Program
Rotate each ring of matrix anticlockwise by K elements
*/
class MyMatrix {
var rows : Int;
var cols : Int;
init(_ matrix: [[Int]]) {
//Get the size of matrix
self.rows = matrix.count;
self.cols = matrix[0].count;
}
func change(_ matrix: inout [[Int]], _ row: Int, _ col: Int, _ index: Int) {
var data: Int = matrix[row][col];
var temp: Int = 0;
//bottom right to top rigth
var i: Int = row - 1;
while (i >= index) {
temp = matrix[i][col];
matrix[i][col] = data;
data = temp;
i -= 1;
}
//top right to top left
i = col - 1;
while (i >= index) {
temp = matrix[index][i];
matrix[index][i] = data;
data = temp;
i -= 1;
}
//top left to bottom left
i = index + 1;
while (i <= row) {
temp = matrix[i][index];
matrix[i][index] = data;
data = temp;
i += 1;
}
//bottom left to bottom right
i = index + 1;
while (i <= col) {
temp = matrix[row][i];
matrix[row][i] = data;
data = temp;
i += 1;
}
}
func rotate(_ matrix: inout [[Int]], _ size: Int) {
var row: Int = self.rows;
var col: Int = self.cols;
var occurrence: Int = self.cols;
if (row < col) {
//Get small size
//When not a perfect square matrix
//Get small size
occurrence = row;
}
//This is rotating each ring by one size outer to inner side
/*
//outer ring
← ← ← ← ← ←
↓             ↑
↓   same as   ↑
↓             ↑
↓  inner ring ↑
↓             ↑
↓             ↑
→ → → → → →
*/
var i: Int = 0;
while (i < occurrence / 2) {
row -= 1;
col -= 1;
self.change(&matrix, row, col, i);
i += 1;
}
if (size > 1) {
self.rotate(&matrix, size - 1);
}
}
//Display matrix elements
func show_data(_ matrix: [
[Int]
]) {
var i: Int = 0;
while (i < self.rows) {
var j: Int = 0;
while (j < self.cols) {
print(" ", matrix[i][j], terminator: "");
j += 1;
}
print("\n", terminator: "");
i += 1;
}
print("\n", terminator: "");
}
}
func main() {
var matrix: [
[Int]
] = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]
];
let obj: MyMatrix = MyMatrix(matrix);
let size: Int = 3;
print("Before\n", terminator: "");
obj.show_data(matrix);
obj.rotate(&matrix, size);
print("After rotate ring in ", size ," times\n", terminator: "");
obj.show_data(matrix);
}
main();```
```

#### Output

``````Before
1  2  3  4  5
6  7  8  9  10
11  12  13  14  15
16  17  18  19  20

After rotate ring in  3  times
4  5  10  15  20
3  14  13  12  19
2  9  8  7  18
1  6  11  16  17
``````
``````<?php
/*
Php Program
Rotate each ring of matrix anticlockwise by K elements
*/
class MyMatrix {
public \$rows;
public \$cols;

function __construct(\$matrix) {
//Get the size of matrix
\$this->rows = count(\$matrix);
\$this->cols = count(\$matrix[0]);
}
public 	function change(&\$matrix, \$row, \$col, \$index) {
\$data = \$matrix[\$row][\$col];
\$temp = 0;
//bottom right to top rigth

for (\$i = \$row - 1; \$i >= \$index; --\$i) {
\$temp = \$matrix[\$i][\$col];
\$matrix[\$i][\$col] = \$data;
\$data = \$temp;
}
//top right to top left

for (\$i = \$col - 1; \$i >= \$index; --\$i) {
\$temp = \$matrix[\$index][\$i];
\$matrix[\$index][\$i] = \$data;
\$data = \$temp;
}
//top left to bottom left

for (\$i = \$index + 1; \$i <= \$row; ++\$i) {
\$temp = \$matrix[\$i][\$index];
\$matrix[\$i][\$index] = \$data;
\$data = \$temp;
}
//bottom left to bottom right

for (\$i = \$index + 1; \$i <= \$col; ++\$i) {
\$temp = \$matrix[\$row][\$i];
\$matrix[\$row][\$i] = \$data;
\$data = \$temp;
}
}

function rotate(&\$matrix, \$size) {
\$row = \$this->rows;
\$col = \$this->cols;
\$occurrence = \$this->cols;
if (\$row < \$col) {
//When not a perfect square matrix
//Get small size
\$occurrence = \$row;
}
//This is rotating each ring by one size outer to inner side
/*
//outer ring
← ← ← ← ← ←
↓             ↑
↓   same as   ↑
↓             ↑
↓  inner ring ↑
↓             ↑
↓             ↑
→ → → → → →
*/

for (\$i = 0; \$i < intval(\$occurrence / 2); ++\$i) {
\$row--;
\$col--;
\$this->change(\$matrix, \$row, \$col, \$i);
}
if (\$size > 1) {
\$this->rotate(\$matrix, \$size - 1);
}
}
//Display matrix elements

public 	function show_data(\$matrix) {
for (\$i = 0; \$i < \$this->rows; ++\$i) {
for (\$j = 0; \$j < \$this->cols; ++\$j) {
echo(" ". \$matrix[\$i][\$j]);
}
echo("\n");
}
echo("\n");
}
};

function main() {
\$matrix = array(
array(1, 2, 3, 4, 5),
array(6, 7, 8, 9, 10),
array(11, 12, 13, 14, 15),
array(16, 17, 18, 19, 20)
);
\$obj = new MyMatrix(\$matrix);
\$size = 3;
echo("Before\n");
\$obj->show_data(\$matrix);
\$obj->rotate(\$matrix, \$size);
echo("After rotate ring in ". \$size ." times\n");
\$obj->show_data(\$matrix);

}
main();```
```

#### Output

``````Before
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20

After rotate ring in 3 times
4 5 10 15 20
3 14 13 12 19
2 9 8 7 18
1 6 11 16 17
``````

## Output Explanation

The given matrix is displayed, and then the output shows the matrix after rotating each ring by 3 elements in an anticlockwise direction. The elements within each ring have been rearranged accordingly.

## Time Complexity

The time complexity of this algorithm depends on the number of rings in the matrix and the size of each ring. The algorithm iterates through each ring and performs a constant number of operations for each element within the ring. Therefore, the overall time complexity can be approximated as O(N), where N is the total number of elements in the matrix.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post