Reverse column elements of diamond shape in matrix
The problem is to reverse the column elements of a diamond shape in a given square matrix. The diamond shape is formed by selecting a central row and gradually decreasing the number of columns on both sides. The goal is to reverse the elements of each column in this diamond shape while keeping the rest of the matrix unchanged.
Example
Consider the following 5x5 matrix:
1 2 3 4 5
6 -7 8 2 10
2 12 13 15 16
17 2 19 20 21
22 23 24 25 2
To reverse the column elements of the diamond shape, we first identify the diamond shape by selecting a central row (in this case, the 3rd row) and decreasing the number of columns on both sides. The diamond shape in this case is:
3
-7 8 2
2 12 13 15 16
2 19 20
24
After reversing the column elements within the diamond shape, the matrix becomes:
24
2 19 20
2 12 13 15 16
-7 8 2
3
Idea to Solve the Problem
To reverse the column elements of the diamond shape in the matrix, we can follow these steps:
- Define a class
MyMatrix
to handle matrix operations. - Calculate the central row index, which is
this.rows / 2
(assumingrows
andcols
are equal since it's a square matrix). - Loop through each row of the matrix:
a. Calculate the number of columns in the current diamond shape, which is
(this.cols) - 2 * row
. b. Call thereverse
function to reverse the column elements within the diamond shape. - Print the matrix after reversing the column elements.
- To reverse the column elements within the diamond shape: a. Loop through the columns within the diamond shape: i. Swap the element in the current column with the corresponding element in the opposite column, using the central row as a reference.
Pseudocode
reverse(matrix, row, col):
auxiliary = 0
for i from row to (this.rows) - row - i and (this.rows) - 1 - row - j > i:
auxiliary = matrix[i][col]
matrix[i][col] = matrix[(this.rows) - 1 - row - j][col]
matrix[(this.rows) - 1 - row - j][col] = auxiliary
reverse_element(matrix):
for i from 0 to this.rows:
reverse(matrix, i, row)
if i < this.rows / 2:
row = row - 1
else:
row = row + 1
main():
matrix = ... # Define the matrix elements
obj = MyMatrix(matrix)
obj.diamond_view(matrix)
obj.reverse_element(matrix)
obj.diamond_view(matrix)
Code Solution
/*
C Program
+ Reverse column elements of diamond shape in matrix
*/
#include<stdio.h>
#define ROW 5
#define COL 5
int element_size(int col)
{
int counter=0;
while(col>0)
{
counter+=col;
col--;
}
return counter*4;
}
void show(int arr[ROW][COL],int row,int col)
{
for (int i = col; i < (COL)-col; ++i)
{
printf("%3d",arr[row][i] );
}
}
void space(int size)
{
for (int i = 0; i <= size; ++i)
{
printf(" ");
}
}
void diamond_view(int arr[ROW][COL])
{
if(ROW != COL || COL%2==0)
{
printf("\nNot a valid perfect Odd square matrix");
return;
}
int col = COL / 2;
int counter = col*3;
int distance = 3;
for (int i = 0; i < ROW; ++i)
{
space(counter);
show(arr,i,col);
if(i < ROW/2)
{
counter -= distance;
col--;
}
else
{
col++;
counter += distance;
}
printf("\n");
}
}
//Reverse the horizontal column elements
void reverse(int arr[ROW][COL],int col,int row)
{
int auxiliary=0;
for (int i = row,j=0; i < (ROW)-row-i && (ROW-1)-row-j > i; ++i,j++)
{
//Swap node element values
auxiliary=arr[i][col];
arr[i][col]= arr[(ROW-1)-row-j][col];
arr[(ROW-1)-row-j][col]=auxiliary;
}
}
void reverse_element(int arr[ROW][COL])
{
if(ROW != COL || COL%2==0)
{
printf("\nNot a valid perfect Odd square matrix");
return;
}
int row = COL / 2;
for (int i = 0; i < ROW; ++i)
{
reverse(arr,i,row);
if(i < ROW/2)
{
row--;
}
else
{
row++;
}
}
printf("\n");
}
int main(){
int matrix[ROW][COL] =
{
{1, 2, 3, 4, 5},
{6, -7, 8, 2, 10},
{2, 12, 13, 15, 16},
{17, 2, 19, 20, 21},
{22, 23, 24, 25, 2}
};
diamond_view(matrix);
reverse_element(matrix);
diamond_view(matrix);
return 0;
}
Output
3
-7 8 2
2 12 13 15 16
2 19 20
24
24
2 19 20
2 12 13 15 16
-7 8 2
3
/*
C++ Program
Reverse column elements of diamond shape in 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;
}
int element_size(int col) {
int counter = 0;
while (col > 0) {
counter += col;
col--;
}
return counter *4;
}
void show(int matrix[][COL], int row, int col) {
for (int i = col; i < (this->cols) - col; ++i) {
cout << " " << matrix[row][i];
}
}
void space(int size) {
for (int i = 0; i <= size; ++i) {
cout << " ";
}
}
void diamond_view(int matrix[][COL]) {
if (this->rows != this->cols || this->cols % 2 == 0) {
cout << "\nNot a valid perfect Odd square matrix";
return;
}
int col = this->cols / 2;
int counter = col *3;
int distance = 3;
for (int i = 0; i < this->rows; ++i) {
this->space(counter);
this->show(matrix, i, col);
if (i < this->rows / 2) {
counter -= distance;
col--;
} else {
col++;
counter += distance;
}
cout << "\n";
}
}
//Reverse the horizontal column elements
void reverse(int matrix[][COL], int col, int row) {
int auxiliary = 0;
for (int i = row, j = 0; i < (this->rows) - row - i && (this->rows - 1) - row - j > i; ++i, j++) {
//Swap node element values
auxiliary = matrix[i][col];
matrix[i][col] = matrix[(this->rows - 1) - row - j][col];
matrix[(this->rows - 1) - row - j][col] = auxiliary;
}
}
void reverse_element(int matrix[][COL]) {
if (this->rows != this->cols || this->cols % 2 == 0) {
cout << "\nNot a valid perfect Odd square matrix";
return;
}
int row = this->cols / 2;
for (int i = 0; i < this->rows; ++i) {
this->reverse(matrix, i, row);
if (i < this->rows / 2) {
row--;
} else {
row++;
}
}
cout << "\n";
}
};
int main() {
int matrix[][COL] = {
{
1,
2,
3,
4,
5
},
{
6,
-7,
8,
2,
10
},
{
2,
12,
13,
15,
16
},
{
17,
2,
19,
20,
21
},
{
22,
23,
24,
25,
2
}
};
MyMatrix obj;
obj.diamond_view(matrix);
obj.reverse_element(matrix);
obj.diamond_view(matrix);
return 0;
}
Output
3
-7 8 2
2 12 13 15 16
2 19 20
24
24
2 19 20
2 12 13 15 16
-7 8 2
3
/*
Java Program
Reverse column elements of diamond shape in matrix
*/
public class MyMatrix {
public int rows;
public int cols;
public MyMatrix(int [][]matrix)
{
//Get the size of matrix
this.rows = matrix.length;
this.cols = matrix[0].length;
}
public int element_size(int col)
{
int counter=0;
while(col>0)
{
counter+=col;
col--;
}
return counter*4;
}
public void show(int [][]matrix,int row,int col)
{
for (int i = col; i < (this.cols)-col; ++i)
{
System.out.print(" "+matrix[row][i] );
}
}
public void space(int size)
{
for (int i = 0; i <= size; ++i)
{
System.out.print(" ");
}
}
public void diamond_view(int [][]matrix)
{
if(this.rows != this.cols || this.cols%2==0)
{
System.out.print("\nNot a valid perfect Odd square matrix");
return;
}
int col = this.cols / 2;
int counter = col*3;
int distance = 3;
for (int i = 0; i < this.rows; ++i)
{
space(counter);
show(matrix,i,col);
if(i < this.rows/2)
{
counter -= distance;
col--;
}
else
{
col++;
counter += distance;
}
System.out.print("\n");
}
}
//Reverse the horizontal column elements
public void reverse(int [][]matrix,int col,int row)
{
int auxiliary=0;
for (int i = row,j=0; i < (this.rows)-row-i && (this.rows-1)-row-j > i; ++i,j++)
{
//Swap node element values
auxiliary=matrix[i][col];
matrix[i][col]= matrix[(this.rows-1)-row-j][col];
matrix[(this.rows-1)-row-j][col]=auxiliary;
}
}
public void reverse_element(int [][]matrix)
{
if(this.rows != this.cols || this.cols%2==0)
{
System.out.print("\nNot a valid perfect Odd square matrix");
return;
}
int row = this.cols / 2;
for (int i = 0; i < this.rows; ++i)
{
reverse(matrix,i,row);
if(i < this.rows/2)
{
row--;
}
else
{
row++;
}
}
System.out.print("\n");
}
public static void main(String[] args)
{
//Define matrix element
int [][]matrix =
{
{1, 2, 3, 4, 5},
{6, -7, 8, 2, 10},
{2, 12, 13, 15, 16},
{17, 2, 19, 20, 21},
{22, 23, 24, 25, 2}
};
MyMatrix obj = new MyMatrix(matrix);
obj.diamond_view(matrix);
obj.reverse_element(matrix);
obj.diamond_view(matrix);
}
}
Output
3
-7 8 2
2 12 13 15 16
2 19 20
24
24
2 19 20
2 12 13 15 16
-7 8 2
3
using System;
/*
C# Program
Reverse column elements of diamond shape in matrix
*/
public class MyMatrix {
int rows;
int cols;
MyMatrix(int[,] matrix) {
//Get the size of matrix
this.rows = matrix.GetLength(0);
this.cols = matrix.GetLength(1);
}
public int element_size(int col) {
int counter = 0;
while (col > 0) {
counter += col;
col--;
}
return counter * 4;
}
public void show(int[,] matrix, int row, int col) {
for (int i = col; i < (this.cols) - col; ++i) {
Console.Write(" " + matrix[row,i]);
}
}
public void space(int size) {
for (int i = 0; i <= size; ++i) {
Console.Write(" ");
}
}
public void diamond_view(int[,] matrix) {
if (this.rows != this.cols || this.cols % 2 == 0) {
Console.Write("\nNot a valid perfect Odd square matrix");
return;
}
int col = this.cols / 2;
int counter = col * 3;
int distance = 3;
for (int i = 0; i < this.rows; ++i) {
space(counter);
show(matrix, i, col);
if (i < this.rows / 2) {
counter -= distance;
col--;
} else {
col++;
counter += distance;
}
Console.Write("\n");
}
}
//Reverse the horizontal column elements
public void reverse(int[,] matrix, int col, int row) {
int auxiliary = 0;
for (int i = row, j = 0; i < (this.rows) - row - i && (this.rows - 1) - row - j > i; ++i, j++) {
//Swap node element values
auxiliary = matrix[i,col];
matrix[i,col] = matrix[(this.rows - 1) - row - j,col];
matrix[(this.rows - 1) - row - j,col] = auxiliary;
}
}
public void reverse_element(int[,] matrix) {
if (this.rows != this.cols || this.cols % 2 == 0) {
Console.Write("\nNot a valid perfect Odd square matrix");
return;
}
int row = this.cols / 2;
for (int i = 0; i < this.rows; ++i) {
reverse(matrix, i, row);
if (i < this.rows / 2) {
row--;
} else {
row++;
}
}
Console.Write("\n");
}
public static void Main(String[] args) {
int[,]
//Define matrix element
matrix = {
{
1,
2,
3,
4,
5
},
{
6,
-7,
8,
2,
10
},
{
2,
12,
13,
15,
16
},
{
17,
2,
19,
20,
21
},
{
22,
23,
24,
25,
2
}
};
MyMatrix obj = new MyMatrix(matrix);
obj.diamond_view(matrix);
obj.reverse_element(matrix);
obj.diamond_view(matrix);
}
}
Output
3
-7 8 2
2 12 13 15 16
2 19 20
24
24
2 19 20
2 12 13 15 16
-7 8 2
3
<?php
/*
Php Program
Reverse column elements of diamond shape in 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 element_size($col) {
$counter = 0;
while ($col > 0) {
$counter += $col;
$col--;
}
return $counter *4;
}
public function show($matrix, $row, $col) {
for ($i = $col; $i < ($this->cols) - $col; ++$i) {
echo(" ". $matrix[$row][$i]);
}
}
public function space($size) {
for ($i = 0; $i <= $size; ++$i) {
echo(" ");
}
}
public function diamond_view($matrix) {
if ($this->rows != $this->cols || $this->cols % 2 == 0) {
echo("\nNot a valid perfect Odd square matrix");
return;
}
$col = intval($this->cols / 2);
$counter = $col *3;
$distance = 3;
for ($i = 0; $i < $this->rows; ++$i) {
$this->space($counter);
$this->show($matrix, $i, $col);
if ($i < intval($this->rows / 2)) {
$counter -= $distance;
$col--;
} else {
$col++;
$counter += $distance;
}
echo("\n");
}
}
//Reverse the horizontal column elements
public function reverse(&$matrix, $col, $row) {
$auxiliary = 0;
for ($i = $row, $j = 0; $i < ($this->rows) - $row - $i && ($this->rows - 1) - $row - $j > $i; ++$i, $j++) {
//Swap node element values
$auxiliary = $matrix[$i][$col];
$matrix[$i][$col] = $matrix[($this->rows - 1) - $row - $j][$col];
$matrix[($this->rows - 1) - $row - $j][$col] = $auxiliary;
}
}
public function reverse_element(&$matrix) {
if ($this->rows != $this->cols || $this->cols % 2 == 0) {
echo("\nNot a valid perfect Odd square matrix");
return;
}
$row = intval($this->cols / 2);
for ($i = 0; $i < $this->rows; ++$i) {
$this->reverse($matrix, $i, $row);
if ($i < intval($this->rows / 2)) {
$row--;
} else {
$row++;
}
}
echo("\n");
}
}
function main() {
//Define matrix element
$matrix = array(
array(1, 2, 3, 4, 5),
array(6, -7, 8, 2, 10),
array(2, 12, 13, 15, 16),
array(17, 2, 19, 20, 21),
array(22, 23, 24, 25, 2));
$obj = new MyMatrix($matrix);
$obj->diamond_view($matrix);
$obj->reverse_element($matrix);
$obj->diamond_view($matrix);
}
main();
Output
3
-7 8 2
2 12 13 15 16
2 19 20
24
24
2 19 20
2 12 13 15 16
-7 8 2
3
/*
Node Js Program
Reverse column elements of diamond shape in matrix
*/
class MyMatrix {
constructor(matrix) {
//Get the size of matrix
this.rows = matrix.length;
this.cols = matrix[0].length;
}
element_size(col) {
var counter = 0;
while (col > 0) {
counter += col;
col--;
}
return counter *4;
}
show(matrix, row, col) {
for (var i = col; i < (this.cols) - col; ++i) {
process.stdout.write(" " + matrix[row][i]);
}
}
space(size) {
for (var i = 0; i <= size; ++i) {
process.stdout.write(" ");
}
}
diamond_view(matrix) {
if (this.rows != this.cols || this.cols % 2 == 0) {
process.stdout.write("\nNot a valid perfect Odd square matrix");
return;
}
var col = parseInt(this.cols / 2);
var counter = col *3;
var distance = 3;
for (var i = 0; i < this.rows; ++i) {
this.space(counter);
this.show(matrix, i, col);
if (i < parseInt(this.rows / 2)) {
counter -= distance;
col--;
} else {
col++;
counter += distance;
}
process.stdout.write("\n");
}
}
//Reverse the horizontal column elements
reverse(matrix, col, row) {
var auxiliary = 0;
for (var i = row, j = 0; i < (this.rows) - row - i && (this.rows - 1) - row - j > i; ++i, j++) {
//Swap node element values
auxiliary = matrix[i][col];
matrix[i][col] = matrix[(this.rows - 1) - row - j][col];
matrix[(this.rows - 1) - row - j][col] = auxiliary;
}
}
reverse_element(matrix) {
if (this.rows != this.cols || this.cols % 2 == 0) {
process.stdout.write("\nNot a valid perfect Odd square matrix");
return;
}
var row = parseInt(this.cols / 2);
for (var i = 0; i < this.rows; ++i) {
this.reverse(matrix, i, row);
if (i < parseInt(this.rows / 2)) {
row--;
} else {
row++;
}
}
process.stdout.write("\n");
}
}
function main(args) {
//Define matrix element
var matrix = [
[1, 2, 3, 4, 5],
[6, -7, 8, 2, 10],
[2, 12, 13, 15, 16],
[17, 2, 19, 20, 21],
[22, 23, 24, 25, 2]
];
var obj = new MyMatrix(matrix);
obj.diamond_view(matrix);
obj.reverse_element(matrix);
obj.diamond_view(matrix);
}
main();
Output
3
-7 8 2
2 12 13 15 16
2 19 20
24
24
2 19 20
2 12 13 15 16
-7 8 2
3
# Python 3 Program
# Reverse column elements of diamond shape in matrix
class MyMatrix :
def __init__(self, matrix) :
# Get the size of matrix
self.rows = len(matrix)
self.cols = len(matrix[0])
def element_size(self, col) :
counter = 0
while (col > 0) :
counter += col
col -= 1
return counter * 4
def show(self, matrix, row, col) :
i = col
while (i < (self.cols) - col) :
print(" ", matrix[row][i], end = "")
i += 1
def space(self, size) :
i = 0
while (i <= size) :
print(end = " ")
i += 1
def diamond_view(self, matrix) :
if (self.rows != self.cols or self.cols % 2 == 0) :
print("\nNot a valid perfect Odd square matrix", end = "")
return
col = int(self.cols / 2)
counter = col * 3
distance = 3
i = 0
while (i < self.rows) :
self.space(counter)
self.show(matrix, i, col)
if (i < int(self.rows / 2)) :
counter -= distance
col -= 1
else :
col += 1
counter += distance
print("\n", end = "")
i += 1
# Reverse the horizontal column elements
def reverse(self, matrix, col, row) :
auxiliary = 0
i = row
j = 0
while (i < (self.rows) - row - i and(self.rows - 1) - row - j > i) :
# Swap node element values
auxiliary = matrix[i][col]
matrix[i][col] = matrix[(self.rows - 1) - row - j][col]
matrix[(self.rows - 1) - row - j][col] = auxiliary
i += 1
j += 1
def reverse_element(self, matrix) :
if (self.rows != self.cols or self.cols % 2 == 0) :
print("\nNot a valid perfect Odd square matrix", end = "")
return
row = int(self.cols / 2)
i = 0
while (i < self.rows) :
self.reverse(matrix, i, row)
if (i < int(self.rows / 2)) :
row -= 1
else :
row += 1
i += 1
print("\n", end = "")
def main() :
matrix = [
[1, 2, 3, 4, 5],
[6, -7, 8, 2, 10],
[2, 12, 13, 15, 16],
[17, 2, 19, 20, 21],
[22, 23, 24, 25, 2]
]
obj = MyMatrix(matrix)
obj.diamond_view(matrix)
obj.reverse_element(matrix)
obj.diamond_view(matrix)
if __name__ == "__main__":
main()
Output
3
-7 8 2
2 12 13 15 16
2 19 20
24
24
2 19 20
2 12 13 15 16
-7 8 2
3
# Ruby Program
# Reverse column elements of diamond shape in matrix
class MyMatrix
# Define the accessor and reader of class MyMatrix
attr_reader :rows, :cols
attr_accessor :rows, :cols
def initialize(matrix)
# Get the size of matrix
self.rows = matrix.length
self.cols = matrix[0].length
end
def element_size(col)
counter = 0
while (col > 0)
counter += col
col -= 1
end
return counter * 4
end
def show(matrix, row, col)
i = col
while (i < (self.cols) - col)
print(" ", matrix[row][i])
i += 1
end
end
def space(size)
i = 0
while (i <= size)
print(" ")
i += 1
end
end
def diamond_view(matrix)
if (self.rows != self.cols || self.cols % 2 == 0)
print("\nNot a valid perfect Odd square matrix")
return
end
col = self.cols / 2
counter = col * 3
distance = 3
i = 0
while (i < self.rows)
self.space(counter)
self.show(matrix, i, col)
if (i < self.rows / 2)
counter -= distance
col -= 1
else
col += 1
counter += distance
end
print("\n")
i += 1
end
end
# Reverse the horizontal column elements
def reverse(matrix, col, row)
auxiliary = 0
i = row
j = 0
while (i < (self.rows) - row - i && (self.rows - 1) - row - j > i)
# Swap node element values
auxiliary = matrix[i][col]
matrix[i][col] = matrix[(self.rows - 1) - row - j][col]
matrix[(self.rows - 1) - row - j][col] = auxiliary
i += 1
j += 1
end
end
def reverse_element(matrix)
if (self.rows != self.cols || self.cols % 2 == 0)
print("\nNot a valid perfect Odd square matrix")
return
end
row = self.cols / 2
i = 0
while (i < self.rows)
self.reverse(matrix, i, row)
if (i < self.rows / 2)
row -= 1
else
row += 1
end
i += 1
end
print("\n")
end
end
def main()
matrix = [
[1, 2, 3, 4, 5],
[6, -7, 8, 2, 10],
[2, 12, 13, 15, 16],
[17, 2, 19, 20, 21],
[22, 23, 24, 25, 2]
]
obj = MyMatrix.new(matrix)
obj.diamond_view(matrix)
obj.reverse_element(matrix)
obj.diamond_view(matrix)
end
main()
Output
3
-7 8 2
2 12 13 15 16
2 19 20
24
24
2 19 20
2 12 13 15 16
-7 8 2
3
/*
Scala Program
Reverse column elements of diamond shape in 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 element_size(size: Int): Int = {
var counter: Int = 0;
var col: Int = size;
while (col > 0) {
counter += col;
col -= 1;
}
return counter * 4;
}
def show(matrix: Array[Array[Int]], row: Int, col: Int): Unit = {
var i: Int = col;
while (i < (this.cols) - col) {
print(" " + matrix(row)(i));
i += 1;
}
}
def space(size: Int): Unit = {
var i: Int = 0;
while (i <= size) {
print(" ");
i += 1;
}
}
def diamond_view(matrix: Array[Array[Int]]): Unit = {
if (this.rows != this.cols || this.cols % 2 == 0) {
print("\nNot a valid perfect Odd square matrix");
return;
}
var col: Int = (this.cols / 2).toInt;
var counter: Int = col * 3;
val distance: Int = 3;
var i: Int = 0;
while (i < this.rows) {
this.space(counter);
this.show(matrix, i, col);
if (i < (this.rows / 2).toInt) {
counter -= distance;
col -= 1;
} else {
col += 1;
counter += distance;
}
print("\n");
i += 1;
}
}
//Reverse the horizontal column elements
def reverse(matrix: Array[Array[Int]], col: Int, row: Int): Unit = {
var auxiliary: Int = 0;
var i: Int = row;
var j: Int = 0;
while (i < (this.rows) - row - i && (this.rows - 1) - row - j > i) {
//Swap node element values
auxiliary = matrix(i)(col);
matrix(i)(col) = matrix((this.rows - 1) - row - j)(col);
matrix((this.rows - 1) - row - j)(col) = auxiliary;
i += 1;
j += 1;
}
}
def reverse_element(matrix: Array[Array[Int]]): Unit = {
if (this.rows != this.cols || this.cols % 2 == 0) {
print("\nNot a valid perfect Odd square matrix");
return;
}
var row: Int = (this.cols / 2).toInt;
var i: Int = 0;
while (i < this.rows) {
this.reverse(matrix, i, row);
if (i < (this.rows / 2).toInt) {
row -= 1;
} else {
row += 1;
}
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, 2, 10),
Array(2, 12, 13, 15, 16),
Array(17, 2, 19, 20, 21),
Array(22, 23, 24, 25, 2));
val obj: MyMatrix = new MyMatrix(matrix);
obj.diamond_view(matrix);
obj.reverse_element(matrix);
obj.diamond_view(matrix);
}
}
Output
3
-7 8 2
2 12 13 15 16
2 19 20
24
24
2 19 20
2 12 13 15 16
-7 8 2
3
/*
Swift Program
Reverse column elements of diamond shape in 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 element_size(_ size: Int) -> Int {
var counter: Int = 0;
var col: Int = size;
while (col > 0) {
counter += col;
col -= 1;
}
return counter * 4;
}
func show(_ matrix: [[Int]], _ row: Int, _ col: Int) {
var i: Int = col;
while (i < (self.cols) - col) {
print(" ", matrix[row][i], terminator: "");
i += 1;
}
}
func space(_ size: Int) {
var i: Int = 0;
while (i <= size) {
print(" ", terminator: "");
i += 1;
}
}
func diamond_view(_ matrix: [
[Int]
]) {
if (self.rows != self.cols || self.cols % 2 == 0) {
print("\nNot a valid perfect Odd square matrix", terminator: "");
return;
}
var col: Int = self.cols / 2;
var counter: Int = col * 3;
let distance: Int = 3;
var i: Int = 0;
while (i < self.rows) {
self.space(counter);
self.show(matrix, i, col);
if (i < self.rows / 2) {
counter -= distance;
col -= 1;
} else {
col += 1;
counter += distance;
}
print("\n", terminator: "");
i += 1;
}
}
//Reverse the horizontal column elements
func reverse(_ matrix: inout [[Int]], _ col: Int, _ row: Int) {
var auxiliary: Int = 0;
var i: Int = row;
var j: Int = 0;
while (i < (self.rows) - row - i && (self.rows - 1) - row - j > i) {
//Swap node element values
auxiliary = matrix[i][col];
matrix[i][col] = matrix[(self.rows - 1) - row - j][col];
matrix[(self.rows - 1) - row - j][col] = auxiliary;
i += 1;
j += 1;
}
}
func reverse_element(_ matrix: inout [[Int]]) {
if (self.rows != self.cols || self.cols % 2 == 0) {
print("\nNot a valid perfect Odd square matrix", terminator: "");
return;
}
var row: Int = self.cols / 2;
var i: Int = 0;
while (i < self.rows) {
self.reverse(&matrix, i, row);
if (i < self.rows / 2) {
row -= 1;
} else {
row += 1;
}
i += 1;
}
print("\n", terminator: "");
}
}
func main() {
var matrix: [[Int]] = [
[1, 2, 3, 4, 5],
[6, -7, 8, 2, 10],
[2, 12, 13, 15, 16],
[17, 2, 19, 20, 21],
[22, 23, 24, 25, 2]
];
let obj: MyMatrix = MyMatrix(matrix);
obj.diamond_view(matrix);
obj.reverse_element(&matrix);
obj.diamond_view(matrix);
}
main();
Output
3
-7 8 2
2 12 13 15 16
2 19 20
24
24
2 19 20
2 12 13 15 16
-7 8 2
3
Output Explanation
The mentioned Java code implements the above algorithm to reverse the column elements of the diamond shape in the
matrix. It correctly identifies the diamond shape by selecting a central row and decreasing the number of columns on
both sides. It then reverses the column elements within the diamond shape using the reverse
function.
The output matches the expected reversed diamond shape matrix.
Time Complexity
The time complexity of the provided solution is O(N^2), where N is the number of rows (or columns) in the square
matrix. The reverse_element
function iterates through each row of the matrix, and for each row, it
reverses the column elements within the diamond shape. Since each element within the diamond shape is reversed
exactly once, the overall time complexity is O(N^2).
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.
New Comment