Posted on by Kalkicode
Code Matrix

# Reverse the diamond elements in square matrix

The given problem involves reversing the elements in a diamond pattern within a square matrix. The goal is to create a function that takes a square matrix as input and rearranges its elements in a way that the elements along the diagonals form a diamond shape, and then reverse the order of those diamond elements.

## Problem Statement

You are given a square matrix with odd dimensions. You need to reverse the diamond elements within the matrix. The diamond elements are the elements along the diagonals that form a diamond shape. For example, in a 5x5 matrix:

``````
1  2  3  4  5
6  7  8  9  10
11  12  13  15  16
17  18  19  20  21
22  23  24  25  26

# Diamond of matrix
3
7  8  9
11 12 13 15 16
18 19 20
24

# When Reverse by row of diamond
3
9  8  7
16 15 13 12 11
20 19 18
24
# Final result

1  2  3  4  5
6  9  8  7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26``````

## Solution Approach

To solve this problem, the code provides a function `reverse_row` that takes the matrix as input and performs the following steps:

1. Check if the matrix is square and has odd dimensions. If it doesn't satisfy these conditions, the function will not proceed.
2. Initialize a variable `col_s` to the middle column index.
3. Loop through each row of the matrix: a. Call the `change` function to reverse the elements in the diamond shape for the current row. b. Decrease `col_s` for the first half of the rows and increase it for the second half.

The `change` function swaps the elements along the diagonal forming the diamond shape. It iterates through half of the row and swaps the elements with their corresponding elements on the opposite side of the diamond.

## Pseudocode

``````function change(matrix, row_s, col_s):
for i from col_s to (COL - 1) - col_s:
swap matrix[row_s][i] with matrix[row_s][COL - 1 - i]

function reverse_row(matrix):
if ROW != COL or COL is even:
print "This is not a perfect odd rows or columns matrix"
return
col_s = COL / 2
for i from 0 to ROW - 1:
call change(matrix, i, col_s)
if i < ROW / 2:
col_s = col_s - 1
else:
col_s = col_s + 1

matrix = predefined square matrix
show_data(matrix)
reverse_row(matrix)
show_data(matrix)``````

## Algorithm Explanation

1. The `change` function iterates through half of the row's elements from `col_s` to `(COL - 1) - col_s`.
2. It swaps the current element with the element at the corresponding position on the opposite side of the diamond.
3. The `reverse_row` function first checks if the matrix is square and has odd dimensions. If not, it prints an error message and returns.
4. It initializes `col_s` to the middle column index.
5. The loop in the `reverse_row` function iterates through each row of the matrix: a. It calls the `change` function to reverse the diamond elements in the current row. b. For the first half of the rows, `col_s` is decreased to move inward through the diamond shape. For the second half, it's increased to move outward.

## Code Solution

``````/*
C Program
+ Reverse the diamond elements in square matrix
*/
#include <stdio.h>
//Size of matrix
#define ROW 5
#define COL 5

void change(int matrix[ROW][COL],int row_s,int col_s)
{
int temp = 0;
for (int i = col_s; i < (COL)-i; ++i)
{
//Swap element value
temp=matrix[row_s][i];
matrix[row_s][i] = matrix[row_s][(COL-1)-i];
matrix[row_s][(COL-1)-i]=temp;
}
}
//Reversing diamond position element in row wise
void reverse_row(int matrix[ROW][COL])
{
if(ROW != COL || COL%2==0)
{
printf("\nThis is not a perfect odd rows or columns matrix");
return;
}

int col_s = COL / 2;

for (int i = 0; i < ROW; ++i)
{
change(matrix,i,col_s);

if(i < ROW/2)
{

col_s--;
}
else
{
col_s++;

}
}

}
//Display matrix elements
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[ROW][COL] ={
{1,  2,   3,  4,  5},
{6,  7,   8,  9,  10},
{11, 12, 13, 15,  16},
{17, 18, 19, 20,  21},
{22, 23, 24, 25,  26}

};

show_data(matrix);

reverse_row(matrix);

show_data(matrix);

return 0;
}```
```

#### Output

``````  1  2  3  4  5
6  7  8  9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1  2  3  4  5
6  9  8  7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26
``````
``````/*
C++ Program
Reverse the diamond elements in square matrix
*/
#include<iostream>
#define ROW 5
#define COL 5
using namespace std;

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_s, int col_s) {
int temp = 0;
for (int i = col_s; i < this->cols - i; ++i) {
//Swap element value
temp = matrix[row_s][i];
matrix[row_s][i] = matrix[row_s][(this->cols - 1) - i];
matrix[row_s][(this->cols - 1) - i] = temp;
}
}
//Reversing diamond position element in row wise
void reverse_row(int matrix[][COL]) {
if (this->rows != this->cols || this->cols % 2 == 0) {
cout << "\nThis is not a perfect odd rows or columns matrix";
return;
}
int col_s = this->cols / 2;
for (int i = 0; i < this->rows; ++i) {
this->change(matrix, i, col_s);
if (i < this->rows / 2) {
col_s--;
} else {
col_s++;
}
}
}
//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,
15,
16
},
{
17,
18,
19,
20,
21
},
{
22,
23,
24,
25,
26
}
};
MyMatrix obj;
obj.show_data(matrix);
obj.reverse_row(matrix);
obj.show_data(matrix);
return 0;
}```
```

#### Output

`````` 1 2 3 4 5
6 7 8 9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1 2 3 4 5
6 9 8 7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26
``````
``````/*
C# Program
Reverse the diamond elements in square matrix
*/
using System;
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_s, int col_s) {
int temp = 0;
for (int i = col_s; i < cols - i; ++i) {
//Swap element value
temp = matrix[row_s,i];
matrix[row_s,i] = matrix[row_s,(cols - 1) - i];
matrix[row_s,(cols - 1) - i] = temp;
}
}
//Reversing diamond position element in row wise
public void reverse_row(int[,] matrix) {
if (rows != cols || cols % 2 == 0) {
Console.Write("\nThis is not a perfect odd rows or columns matrix");
return;
}
int col_s = cols / 2;
for (int i = 0; i < rows; ++i) {
change(matrix, i, col_s);
if (i < rows / 2) {
col_s--;
} else {
col_s++;
}
}
}
//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[,]
//Define matrix
matrix = {
{
1,
2,
3,
4,
5
},
{
6,
7,
8,
9,
10
},
{
11,
12,
13,
15,
16
},
{
17,
18,
19,
20,
21
},
{
22,
23,
24,
25,
26
}
};
MyMatrix obj = new MyMatrix(matrix);
obj.show_data(matrix);
obj.reverse_row(matrix);
obj.show_data(matrix);
}
}```
```

#### Output

`````` 1 2 3 4 5
6 7 8 9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1 2 3 4 5
6 9 8 7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26
``````
``````<?php
/*
Php Program
Reverse the diamond elements in square matrix
*/
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_s, \$col_s) {
\$temp = 0;
for (\$i = \$col_s; \$i < \$this->cols - \$i; ++\$i) {
//Swap element value
\$temp = \$matrix[\$row_s][\$i];
\$matrix[\$row_s][\$i] = \$matrix[\$row_s][(\$this->cols - 1) - \$i];
\$matrix[\$row_s][(\$this->cols - 1) - \$i] = \$temp;
}
}
//Reversing diamond position element in row wise

public 	function reverse_row(&\$matrix) {
if (\$this->rows != \$this->cols || \$this->cols % 2 == 0) {
echo("\nThis is not a perfect odd rows or columns matrix");
return;
}
\$col_s = intval(\$this->cols / 2);
for (\$i = 0; \$i < \$this->rows; ++\$i) {
\$this->change(\$matrix, \$i, \$col_s);
if (\$i < intval(\$this->rows / 2)) {
\$col_s--;
} else {
\$col_s++;
}
}
}
//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() {
//Define matrix
\$matrix = array(
array(1, 2, 3, 4, 5),
array(6, 7, 8, 9, 10),
array(11, 12, 13, 15, 16),
array(17, 18, 19, 20, 21),
array(22, 23, 24, 25, 26)
);
\$obj = new MyMatrix(\$matrix);
\$obj->show_data(\$matrix);
\$obj->reverse_row(\$matrix);
\$obj->show_data(\$matrix);

}
main();```
```

#### Output

`````` 1 2 3 4 5
6 7 8 9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1 2 3 4 5
6 9 8 7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26
``````
``````/*
Node Js Program
Reverse the diamond elements in square matrix
*/
class MyMatrix {
;;
constructor(matrix) {
//Get the size of matrix
this.rows = matrix.length;
this.cols = matrix[0].length;
}
change(matrix, row_s, col_s) {
var temp = 0;
for (var i = col_s; i < this.cols - i; ++i) {
//Swap element value
temp = matrix[row_s][i];
matrix[row_s][i] = matrix[row_s][(this.cols - 1) - i];
matrix[row_s][(this.cols - 1) - i] = temp;
}
}

//Reversing diamond position element in row wise
reverse_row(matrix) {
if (this.rows != this.cols || this.cols % 2 == 0) {
process.stdout.write("\nThis is not a perfect odd rows or columns matrix");
return;
}
var col_s = parseInt(this.cols / 2);
for (var i = 0; i < this.rows; ++i) {
this.change(matrix, i, col_s);
if (i < parseInt(this.rows / 2)) {
col_s--;
} else {
col_s++;
}
}
}

//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) {
//Define matrix
var matrix = [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 15, 16],
[17, 18, 19, 20, 21],
[22, 23, 24, 25, 26]
];
var obj = new MyMatrix(matrix);
obj.show_data(matrix);
obj.reverse_row(matrix);
obj.show_data(matrix);
}
main();```
```

#### Output

`````` 1 2 3 4 5
6 7 8 9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1 2 3 4 5
6 9 8 7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26
``````
``````# Python 3 Program
# Reverse the diamond elements in square matrix
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_s, col_s) :
temp = 0
i = col_s
while (i < self.cols - i) :
# Swap element value
temp = matrix[row_s][i]
matrix[row_s][i] = matrix[row_s][(self.cols - 1) - i]
matrix[row_s][(self.cols - 1) - i] = temp
i += 1

# Reversing diamond position element in row wise
def reverse_row(self, matrix) :
if (self.rows != self.cols or self.cols % 2 == 0) :
print("\nThis is not a perfect odd rows or columns matrix", end = "")
return

col_s = int(self.cols / 2)
i = 0
while (i < self.rows) :
self.change(matrix, i, col_s)
if (i < int(self.rows / 2)) :
col_s -= 1
else :
col_s += 1

i += 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, 15, 16],
[17, 18, 19, 20, 21],
[22, 23, 24, 25, 26]
]
obj = MyMatrix(matrix)
obj.show_data(matrix)
obj.reverse_row(matrix)
obj.show_data(matrix)

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

#### Output

``````  1  2  3  4  5
6  7  8  9  10
11  12  13  15  16
17  18  19  20  21
22  23  24  25  26

1  2  3  4  5
6  9  8  7  10
16  15  13  12  11
17  20  19  18  21
22  23  24  25  26
``````
``````# Ruby Program
# Reverse the diamond elements in square matrix
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_s, col_s)
temp = 0
i = col_s
while (i < @cols - i)
# Swap element value
temp = matrix[row_s][i]
matrix[row_s][i] = matrix[row_s][(@cols - 1) - i]
matrix[row_s][(@cols - 1) - i] = temp
i += 1
end
end
# Reversing diamond position element in row wise
def reverse_row(matrix)
if (@rows != @cols or @cols % 2 == 0)
print("\nThis is not a perfect odd rows or columns matrix")
return
end
col_s = @cols / 2
i = 0
while (i < @rows)
self.change(matrix, i, col_s)
if (i < @rows / 2)
col_s -= 1
else
col_s += 1
end
i += 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, 15, 16],
[17, 18, 19, 20, 21],
[22, 23, 24, 25, 26]
]
obj = MyMatrix.new(matrix)
obj.show_data(matrix)
obj.reverse_row(matrix)
obj.show_data(matrix)
end

main()```
```

#### Output

`````` 1 2 3 4 5
6 7 8 9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1 2 3 4 5
6 9 8 7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26

``````
``````/*
Scala Program
Reverse the diamond elements in square matrix
*/
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_s: Int, col_s: Int): Unit = {
var temp: Int = 0;
var i: Int = col_s;
while (i < this.cols - i) {
//Swap element value
temp = matrix(row_s)(i);
matrix(row_s)(i) = matrix(row_s)((this.cols - 1) - i);
matrix(row_s)((this.cols - 1) - i) = temp;
i += 1;
}
}
//Reversing diamond position element in row wise
def reverse_row(matrix: Array[Array[Int]]): Unit = {
if (this.rows != this.cols || this.cols % 2 == 0) {
print("\nThis is not a perfect odd rows or columns matrix");

return;
}
var col_s: Int = (this.cols / 2).toInt;
var i: Int = 0;
while (i < this.rows) {
this.change(matrix, i, col_s);

if (i < (this.rows / 2).toInt) {
col_s -= 1;
} else {
col_s += 1;
}
i += 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, 15, 16),
Array(17, 18, 19, 20, 21),
Array(22, 23, 24, 25, 26));
val obj: MyMatrix = new MyMatrix(matrix);
obj.show_data(matrix);
obj.reverse_row(matrix);
obj.show_data(matrix);
}
}```
```

#### Output

`````` 1 2 3 4 5
6 7 8 9 10
11 12 13 15 16
17 18 19 20 21
22 23 24 25 26

1 2 3 4 5
6 9 8 7 10
16 15 13 12 11
17 20 19 18 21
22 23 24 25 26
``````
``````/*
Swift 4 Program
Reverse the diamond elements in square matrix
*/
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_s: Int, _ col_s: Int) {
var temp: Int = 0;
var i: Int = col_s;
while (i < self.cols - i) {
//Swap element value
temp = matrix[row_s][i];
matrix[row_s][i] = matrix[row_s][(self.cols - 1) - i];
matrix[row_s][(self.cols - 1) - i] = temp;
i += 1;
}
}
//Reversing diamond position element in row wise
func reverse_row(_ matrix: inout [[Int]]) {
if (self.rows != self.cols || self.cols % 2 == 0) {
print("\nThis is not a perfect odd rows or columns matrix", terminator: "");
return;
}
var col_s: Int = self.cols / 2;
var i: Int = 0;
while (i < self.rows) {
self.change(&matrix, i, col_s);
if (i < self.rows / 2) {
col_s -= 1;
} else {
col_s += 1;
}
i += 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, 15, 16],
[17, 18, 19, 20, 21],
[22, 23, 24, 25, 26]
];
let obj: MyMatrix = MyMatrix(matrix);
obj.show_data(matrix);
obj.reverse_row(&matrix);
obj.show_data(matrix);
}
main();```
```

#### Output

``````  1  2  3  4  5
6  7  8  9  10
11  12  13  15  16
17  18  19  20  21
22  23  24  25  26

1  2  3  4  5
6  9  8  7  10
16  15  13  12  11
17  20  19  18  21
22  23  24  25  26
``````

## Time Complexity

The `change` function has a time complexity of O(N), where N is the number of columns in the matrix. The `reverse_row` function iterates through each row and calls the `change` function, resulting in a time complexity of O(N^2), where N is the number of rows/columns in the matrix. The overall time complexity of the given code is O(N^2).

## 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