Replace corner element in matrix
The problem at hand is to replace the corner elements of a square matrix with elements from the opposite corners. Specifically, we want to swap the elements of the left-top corner with the elements of the bottom-right corner and vice versa, and then swap the elements of the right-top corner with the elements of the bottom-left corner and vice versa.
Problem Statement and Example
Consider the following square matrix:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
After replacing the corner elements, the matrix will look like this:
49 42 35 4 29 36 43
48 41 10 11 12 37 44
47 16 17 18 19 20 45
22 23 24 25 26 27 28
5 30 31 32 33 34 3
6 13 38 39 40 9 2
7 14 21 46 15 8 1
Idea to Solve the Problem
To replace the corner elements in the square matrix, we can follow the following approach:
- First, we need to check if the given matrix is a valid odd-sized square matrix. If it is not, we return as the problem cannot be solved with an invalid matrix.
- We find the middle element of the matrix, which will be the index for dividing the matrix into the four corners (left-top, right-top, left-bottom, and right-bottom).
- We perform two operations: a. Move elements from the left-top corner to the bottom-right corner and vice versa. b. Move elements from the right-top corner to the bottom-left corner and vice versa.
- The 'left_to_bottom' function handles the first operation, where we traverse the left-top corner to the bottom-right corner and swap the elements with the corresponding elements in the opposite corner.
- The 'right_to_top' function handles the second operation, where we traverse the right-top corner to the bottom-left corner and swap the elements with the corresponding elements in the opposite corner.
Pseudocode
left_to_bottom(matrix, mid):
for i from mid + 1 to cols:
for j from 0 to mid:
swap(matrix[j][i + j], matrix[rows - (i + j) - 1][j])
right_to_top(matrix, mid):
for i from mid - 1 to 0:
for j from 0 to mid:
swap(matrix[j][i - j], matrix[rows - (i - j) - 1][cols - 1 - j])
replace_corner(matrix):
mid = cols / 2
if rows != cols or cols % 2 == 0:
print "Not a valid perfect Odd square matrix"
return
left_to_bottom(matrix, mid)
right_to_top(matrix, mid)
Algorithm Explanation
- The 'left_to_bottom' function takes 'matrix' and 'mid' as input parameters. 'matrix' is the square matrix, and 'mid' is the middle element index of the matrix (rows and columns are 0-based indexed).
- The function traverses the left-top corner from 'mid + 1' to the last column and from 0 to 'mid'.
- For each element in the left-top corner, it swaps it with the corresponding element in the bottom-right corner using the 'swap' function.
- The 'right_to_top' function takes 'matrix' and 'mid' as input parameters. 'matrix' is the square matrix, and 'mid' is the middle element index of the matrix.
- The function traverses the right-top corner from 'mid - 1' to the first column and from 0 to 'mid'.
- For each element in the right-top corner, it swaps it with the corresponding element in the bottom-left corner using the 'swap' function.
- The 'replace_corner' function checks if the matrix is a valid odd-sized square matrix and then calls 'left_to_bottom' and 'right_to_top' functions to replace the corner elements.
Code Solution
/*
C Program
+ Replace corner element in matrix
*/
#include <stdio.h>
#define ROW 7
#define COL 7
//Left top to bottom
void left_to_bottom(int matrix[ROW][COL],int mid)
{
int temp=0;
for (int i = mid+1; i < COL ; ++i)
{
for (int j = 0,k=i; j < mid && i+j < COL && k < ROW; ++j,k++)
{
temp = matrix[j][i+j];
matrix[j][i+j]= matrix[k][j] ;
matrix[k][j] = temp;
}
}
}
void right_to_top(int matrix[ROW][COL],int mid)
{
int temp=0;
for (int i = mid-1,l=mid+1; i >= 0 ; --i,l++)
{
for (int j = 0,k=COL-1; j < mid && i-j >=0 && k >mid && l+j <ROW; ++j,k--)
{
temp = matrix[j][i-j] ;
matrix[j][i-j] = matrix[l+j][k] ;
matrix[l+j][k] = temp;
}
}
}
//Display matrix elements
void show_data(int arr[][COL])
{
for (int i = 0; i < ROW; ++i)
{
for (int j = 0; j < COL; ++j)
{
printf("%4d",arr[i][j] );
}
printf("\n");
}
printf("\n");
}
void replace_corner(int matrix[ROW][COL])
{
if(ROW != COL || COL % 2 == 0)
{
printf("\nNot a valid perfect Odd square matrix");
return;
}
int mid=(COL)/2;
left_to_bottom(matrix,mid);
right_to_top(matrix,mid);
}
int main(){
int matrix[ROW][COL] = {
{1, 2, 3, 4, 5 ,6, 7},
{8, 9, 10, 11, 12, 13, 14},
{15, 16, 17, 18, 19, 20, 21},
{22, 23, 24, 25, 26, 27, 28},
{29, 30, 31, 32, 33, 34, 35},
{36, 37, 38, 39, 40, 41, 42},
{43, 44, 45, 46, 47, 48, 49}
};
show_data(matrix);
replace_corner(matrix);
show_data(matrix);
return 0;
}
Output
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
49 42 35 4 29 36 43
48 41 10 11 12 37 44
47 16 17 18 19 20 45
22 23 24 25 26 27 28
5 30 31 32 33 34 3
6 13 38 39 40 9 2
7 14 21 46 15 8 1
/*
C++ Program
Replace corner element in matrix
*/
#include<iostream>
#define ROW 7
#define COL 7
using namespace std;
class MyMatrix {
public:
int rows;
int cols;
MyMatrix() {
//Get the size of matrix
this->rows = ROW;
this->cols = COL;
}
//Left top to bottom
void left_to_bottom(int matrix[][COL], int mid) {
int temp = 0;
for (int i = mid + 1; i < this->cols; ++i) {
for (int j = 0, k = i; j < mid && i + j < this->cols && k < this->rows; ++j, k++) {
temp = matrix[j][i + j];
matrix[j][i + j] = matrix[k][j];
matrix[k][j] = temp;
}
}
}
void right_to_top(int matrix[][COL], int mid) {
int temp = 0;
for (int i = mid - 1, l = mid + 1; i >= 0; --i, l++) {
for (int j = 0, k = this->cols - 1; j < mid && i - j >= 0 && k > mid && l + j < this->rows; ++j, k--) {
temp = matrix[j][i - j];
matrix[j][i - j] = matrix[l + j][k];
matrix[l + j][k] = temp;
}
}
}
//Display matrix elements
void show_data(int arr[][COL]) {
for (int i = 0; i < this->rows; ++i) {
for (int j = 0; j < this->cols; ++j) {
cout << " " << arr[i][j];
}
cout << "\n";
}
cout << "\n";
}
void replace_corner(int matrix[][COL]) {
if (this->rows != this->cols || this->cols % 2 == 0) {
cout << "\nNot a valid perfect Odd square matrix";
return;
}
int mid = (this->cols) / 2;
this->left_to_bottom(matrix, mid);
this->right_to_top(matrix, mid);
}
};
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,
21
},
{
22,
23,
24,
25,
26,
27,
28
},
{
29,
30,
31,
32,
33,
34,
35
},
{
36,
37,
38,
39,
40,
41,
42
},
{
43,
44,
45,
46,
47,
48,
49
}
};
MyMatrix obj ;
obj.show_data(matrix);
obj.replace_corner(matrix);
obj.show_data(matrix);
return 0;
}
Output
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
49 42 35 4 29 36 43
48 41 10 11 12 37 44
47 16 17 18 19 20 45
22 23 24 25 26 27 28
5 30 31 32 33 34 3
6 13 38 39 40 9 2
7 14 21 46 15 8 1
/*
Java Program
Replace corner element in matrix
*/
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;
}
//Left top to bottom
public void left_to_bottom(int [][]matrix,int mid)
{
int temp=0;
for (int i = mid+1; i < cols ; ++i)
{
for (int j = 0,k=i; j < mid && i+j < cols && k < rows; ++j,k++)
{
temp = matrix[j][i+j];
matrix[j][i+j]= matrix[k][j] ;
matrix[k][j] = temp;
}
}
}
public void right_to_top(int [][]matrix,int mid)
{
int temp=0;
for (int i = mid-1,l=mid+1; i >= 0 ; --i,l++)
{
for (int j = 0,k=cols-1; j < mid && i-j >=0 && k >mid && l+j <rows; ++j,k--)
{
temp = matrix[j][i-j] ;
matrix[j][i-j] = matrix[l+j][k] ;
matrix[l+j][k] = temp;
}
}
}
//Display matrix elements
public void show_data(int [][]arr)
{
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
System.out.print(" "+arr[i][j] );
}
System.out.print("\n");
}
System.out.print("\n");
}
public void replace_corner(int [][]matrix)
{
if(rows != cols || cols % 2 == 0)
{
System.out.print("\nNot a valid perfect Odd square matrix");
return;
}
int mid=(cols)/2;
left_to_bottom(matrix,mid);
right_to_top(matrix,mid);
}
public static void main(String[] args) {
//Define matrix
int [][]matrix = {
{1, 2, 3, 4, 5 ,6, 7},
{8, 9, 10, 11, 12, 13, 14},
{15, 16, 17, 18, 19, 20, 21},
{22, 23, 24, 25, 26, 27, 28},
{29, 30, 31, 32, 33, 34, 35},
{36, 37, 38, 39, 40, 41, 42},
{43, 44, 45, 46, 47, 48, 49}
};
MyMatrix obj = new MyMatrix(matrix);
obj.show_data(matrix);
obj.replace_corner(matrix);
obj.show_data(matrix);
}
}
Output
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
49 42 35 4 29 36 43
48 41 10 11 12 37 44
47 16 17 18 19 20 45
22 23 24 25 26 27 28
5 30 31 32 33 34 3
6 13 38 39 40 9 2
7 14 21 46 15 8 1
/*
C# Program
Replace corner element in 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);
}
//Left top to bottom
public void left_to_bottom(int[,] matrix, int mid) {
int temp = 0;
for (int i = mid + 1; i < cols; ++i) {
for (int j = 0, k = i; j < mid && i + j < cols && k < rows; ++j, k++) {
temp = matrix[j,i + j];
matrix[j,i + j] = matrix[k,j];
matrix[k,j] = temp;
}
}
}
public void right_to_top(int[,] matrix, int mid) {
int temp = 0;
for (int i = mid - 1, l = mid + 1; i >= 0; --i, l++) {
for (int j = 0, k = cols - 1; j < mid && i - j >= 0 && k > mid && l + j < rows; ++j, k--) {
temp = matrix[j,i - j];
matrix[j,i - j] = matrix[l + j,k];
matrix[l + j,k] = temp;
}
}
}
//Display matrix elements
public void show_data(int[,] arr) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
Console.Write(" " + arr[i,j]);
}
Console.Write("\n");
}
Console.Write("\n");
}
public void replace_corner(int[,] matrix) {
if (rows != cols || cols % 2 == 0) {
Console.Write("\nNot a valid perfect Odd square matrix");
return;
}
int mid = (cols) / 2;
left_to_bottom(matrix, mid);
right_to_top(matrix, mid);
}
public static void Main(String[] args) {
int[,]
//Define matrix
matrix = {
{
1,
2,
3,
4,
5,
6,
7
},
{
8,
9,
10,
11,
12,
13,
14
},
{
15,
16,
17,
18,
19,
20,
21
},
{
22,
23,
24,
25,
26,
27,
28
},
{
29,
30,
31,
32,
33,
34,
35
},
{
36,
37,
38,
39,
40,
41,
42
},
{
43,
44,
45,
46,
47,
48,
49
}
};
MyMatrix obj = new MyMatrix(matrix);
obj.show_data(matrix);
obj.replace_corner(matrix);
obj.show_data(matrix);
}
}
Output
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
49 42 35 4 29 36 43
48 41 10 11 12 37 44
47 16 17 18 19 20 45
22 23 24 25 26 27 28
5 30 31 32 33 34 3
6 13 38 39 40 9 2
7 14 21 46 15 8 1
<?php
/*
Php Program
Replace corner element 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]);
}
//Left top to bottom
public function left_to_bottom(&$matrix, $mid) {
$temp = 0;
for ($i = $mid + 1; $i < $this->cols; ++$i) {
for ($j = 0, $k = $i; $j < $mid && $i + $j < $this->cols && $k < $this->rows; ++$j, $k++) {
$temp = $matrix[$j][$i + $j];
$matrix[$j][$i + $j] = $matrix[$k][$j];
$matrix[$k][$j] = $temp;
}
}
}
public function right_to_top(&$matrix, $mid) {
$temp = 0;
for ($i = $mid - 1, $l = $mid + 1; $i >= 0; --$i, $l++) {
for ($j = 0, $k = $this->cols - 1; $j < $mid && $i - $j >= 0 && $k > $mid && $l + $j < $this->rows; ++$j, $k--) {
$temp = $matrix[$j][$i - $j];
$matrix[$j][$i - $j] = $matrix[$l + $j][$k];
$matrix[$l + $j][$k] = $temp;
}
}
}
//Display matrix elements
public function show_data($arr) {
for ($i = 0; $i < $this->rows; ++$i) {
for ($j = 0; $j < $this->cols; ++$j) {
echo(" ". $arr[$i][$j]);
}
echo("\n");
}
echo("\n");
}
public function replace_corner(&$matrix) {
if ($this->rows != $this->cols || $this->cols % 2 == 0) {
echo("\nNot a valid perfect Odd square matrix");
return;
}
$mid = intval(($this->cols) / 2);
$this->left_to_bottom($matrix, $mid);
$this->right_to_top($matrix, $mid);
}
};
function main() {
//Define matrix
$matrix = array(
array(1, 2, 3, 4, 5, 6, 7),
array(8, 9, 10, 11, 12, 13, 14),
array(15, 16, 17, 18, 19, 20, 21),
array(22, 23, 24, 25, 26, 27, 28),
array(29, 30, 31, 32, 33, 34, 35),
array(36, 37, 38, 39, 40, 41, 42),
array(43, 44, 45, 46, 47, 48, 49)
);
$obj = new MyMatrix($matrix);
$obj->show_data($matrix);
$obj->replace_corner($matrix);
$obj->show_data($matrix);
}
main();
Output
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
49 42 35 4 29 36 43
48 41 10 11 12 37 44
47 16 17 18 19 20 45
22 23 24 25 26 27 28
5 30 31 32 33 34 3
6 13 38 39 40 9 2
7 14 21 46 15 8 1
/*
Node Js Program
Replace corner element in matrix
*/
class MyMatrix {
;;
constructor(matrix) {
//Get the size of matrix
this.rows = matrix.length;
this.cols = matrix[0].length;
}
//Left top to bottom
left_to_bottom(matrix, mid) {
var temp = 0;
for (var i = mid + 1; i < this.cols; ++i) {
for (var j = 0,k = i; j < mid && i + j < this.cols && k < this.rows; ++j, k++) {
temp = matrix[j][i + j];
matrix[j][i + j] = matrix[k][j];
matrix[k][j] = temp;
}
}
}
right_to_top(matrix, mid) {
var temp = 0;
for (var i = mid - 1,l = mid + 1; i >= 0; --i, l++) {
for (var j = 0,k = this.cols - 1; j < mid && i - j >= 0 && k > mid && l + j < this.rows; ++j, k--) {
temp = matrix[j][i - j];
matrix[j][i - j] = matrix[l + j][k];
matrix[l + j][k] = temp;
}
}
}
//Display matrix elements
show_data(arr) {
for (var i = 0; i < this.rows; ++i) {
for (var j = 0; j < this.cols; ++j) {
process.stdout.write(" " + arr[i][j]);
}
process.stdout.write("\n");
}
process.stdout.write("\n");
}
replace_corner(matrix) {
if (this.rows != this.cols || this.cols % 2 == 0) {
process.stdout.write("\nNot a valid perfect Odd square matrix");
return;
}
var mid = parseInt((this.cols) / 2);
this.left_to_bottom(matrix, mid);
this.right_to_top(matrix, mid);
}
}
function main(args) {
//Define matrix
var matrix = [
[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20, 21],
[22, 23, 24, 25, 26, 27, 28],
[29, 30, 31, 32, 33, 34, 35],
[36, 37, 38, 39, 40, 41, 42],
[43, 44, 45, 46, 47, 48, 49]
];
var obj = new MyMatrix(matrix);
obj.show_data(matrix);
obj.replace_corner(matrix);
obj.show_data(matrix);
}
main();
Output
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
49 42 35 4 29 36 43
48 41 10 11 12 37 44
47 16 17 18 19 20 45
22 23 24 25 26 27 28
5 30 31 32 33 34 3
6 13 38 39 40 9 2
7 14 21 46 15 8 1
# Python 3 Program
# Replace corner element in matrix
class MyMatrix :
def __init__(self, matrix) :
# Get the size of matrix
self.rows = len(matrix)
self.cols = len(matrix[0])
# Left top to bottom
def left_to_bottom(self, matrix, mid) :
temp = 0
i = mid + 1
while (i < self.cols) :
j = 0
k = i
while (j < mid and i + j < self.cols and k < self.rows) :
temp = matrix[j][i + j]
matrix[j][i + j] = matrix[k][j]
matrix[k][j] = temp
j += 1
k += 1
i += 1
def right_to_top(self, matrix, mid) :
temp = 0
i = mid - 1
l = mid + 1
while (i >= 0) :
j = 0
k = self.cols - 1
while (j < mid and i - j >= 0 and k > mid and l + j < self.rows) :
temp = matrix[j][i - j]
matrix[j][i - j] = matrix[l + j][k]
matrix[l + j][k] = temp
j += 1
k -= 1
i -= 1
l += 1
# Display matrix elements
def show_data(self, arr) :
i = 0
while (i < self.rows) :
j = 0
while (j < self.cols) :
print(" ", arr[i][j], end = "")
j += 1
print("\n", end = "")
i += 1
print("\n", end = "")
def replace_corner(self, matrix) :
if (self.rows != self.cols or self.cols % 2 == 0) :
print("\nNot a valid perfect Odd square matrix", end = "")
return
mid = int((self.cols) / 2)
self.left_to_bottom(matrix, mid)
self.right_to_top(matrix, mid)
def main() :
matrix = [
[1, 2, 3, 4, 5, 6, 7],
[8, 9, 10, 11, 12, 13, 14],
[15, 16, 17, 18, 19, 20, 21],
[22, 23, 24, 25, 26, 27, 28],
[29, 30, 31, 32, 33, 34, 35],
[36, 37, 38, 39, 40, 41, 42],
[43, 44, 45, 46, 47, 48, 49]
]
obj = MyMatrix(matrix)
obj.show_data(matrix)
obj.replace_corner(matrix)
obj.show_data(matrix)
if __name__ == "__main__":
main()
Output
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
49 42 35 4 29 36 43
48 41 10 11 12 37 44
47 16 17 18 19 20 45
22 23 24 25 26 27 28
5 30 31 32 33 34 3
6 13 38 39 40 9 2
7 14 21 46 15 8 1
# Ruby Program
# Replace corner element 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
@rows = matrix.length
@cols = matrix[0].length
end
# Left top to bottom
def left_to_bottom(matrix, mid)
temp = 0
i = mid + 1
while (i < @cols)
j = 0
k = i
while (j < mid and i + j < @cols and k < @rows)
temp = matrix[j][i + j]
matrix[j][i + j] = matrix[k][j]
matrix[k][j] = temp
j += 1
k += 1
end
i += 1
end
end
def right_to_top(matrix, mid)
temp = 0
i = mid - 1
l = mid + 1
while (i >= 0)
j = 0
k = @cols - 1
while (j < mid and i - j >= 0 and k > mid and l + j < @rows)
temp = matrix[j][i - j]
matrix[j][i - j] = matrix[l + j][k]
matrix[l + j][k] = temp
j += 1
k -= 1
end
i -= 1
l += 1
end
end
# Display matrix elements
def show_data(arr)
i = 0
while (i < @rows)
j = 0
while (j < @cols)
print(" ", arr[i][j])
j += 1
end
print("\n")
i += 1
end
print("\n")
end
def replace_corner(matrix)
if (@rows != @cols or @cols % 2 == 0)
print("\nNot a valid perfect Odd square matrix")
return
end
mid = (@cols) / 2
self.left_to_bottom(matrix, mid)
self.right_to_top(matrix, mid)
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, 21],
[22, 23, 24, 25, 26, 27, 28],
[29, 30, 31, 32, 33, 34, 35],
[36, 37, 38, 39, 40, 41, 42],
[43, 44, 45, 46, 47, 48, 49]
]
obj = MyMatrix.new(matrix)
obj.show_data(matrix)
obj.replace_corner(matrix)
obj.show_data(matrix)
end
main()
Output
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
49 42 35 4 29 36 43
48 41 10 11 12 37 44
47 16 17 18 19 20 45
22 23 24 25 26 27 28
5 30 31 32 33 34 3
6 13 38 39 40 9 2
7 14 21 46 15 8 1
/*
Scala Program
Replace corner element 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);
}
//Left top to bottom
def left_to_bottom(matrix: Array[Array[Int]], mid: Int): Unit = {
var temp: Int = 0;
var i: Int = mid + 1;
while (i < this.cols) {
var j: Int = 0;
var k: Int = i;
while (j < mid && i + j < this.cols && k < this.rows) {
temp = matrix(j)(i + j);
matrix(j)(i + j) = matrix(k)(j);
matrix(k)(j) = temp;
j += 1;
k += 1;
}
i += 1;
}
}
def right_to_top(matrix: Array[Array[Int]], mid: Int): Unit = {
var temp: Int = 0;
var i: Int = mid - 1;
var l: Int = mid + 1;
while (i >= 0) {
var j: Int = 0;
var k: Int = this.cols - 1;
while (j < mid && i - j >= 0 && k > mid && l + j < this.rows) {
temp = matrix(j)(i - j);
matrix(j)(i - j) = matrix(l + j)(k);
matrix(l + j)(k) = temp;
j += 1;
k -= 1;
}
i -= 1;
l += 1;
}
}
//Display matrix elements
def show_data(arr: Array[Array[Int]]): Unit = {
var i: Int = 0;
while (i < this.rows) {
var j: Int = 0;
while (j < this.cols) {
print(" " + arr(i)(j));
j += 1;
}
print("\n");
i += 1;
}
print("\n");
}
def replace_corner(matrix: Array[Array[Int]]): Unit = {
if (this.rows != this.cols || this.cols % 2 == 0) {
print("\nNot a valid perfect Odd square matrix");
return;
}
val mid: Int = ((this.cols) / 2).toInt;
this.left_to_bottom(matrix, mid);
this.right_to_top(matrix, mid);
}
}
object Main {
def main(args: Array[String]): Unit = {
val matrix: Array[Array[Int]] = Array(
Array(1, 2, 3, 4, 5, 6, 7),
Array(8, 9, 10, 11, 12, 13, 14),
Array(15, 16, 17, 18, 19, 20, 21),
Array(22, 23, 24, 25, 26, 27, 28),
Array(29, 30, 31, 32, 33, 34, 35),
Array(36, 37, 38, 39, 40, 41, 42),
Array(43, 44, 45, 46, 47, 48, 49));
val obj: MyMatrix = new MyMatrix(matrix);
obj.show_data(matrix);
obj.replace_corner(matrix);
obj.show_data(matrix);
}
}
Output
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
49 42 35 4 29 36 43
48 41 10 11 12 37 44
47 16 17 18 19 20 45
22 23 24 25 26 27 28
5 30 31 32 33 34 3
6 13 38 39 40 9 2
7 14 21 46 15 8 1
/*
Swift 4 Program
Replace corner element 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;
}
//Left top to bottom
func left_to_bottom(_ matrix: inout [[Int]], _ mid: Int) {
var temp: Int = 0;
var i: Int = mid + 1;
while (i < self.cols) {
var j: Int = 0;
var k: Int = i;
while (j < mid && i + j < self.cols && k < self.rows) {
temp = matrix[j][i + j];
matrix[j][i + j] = matrix[k][j];
matrix[k][j] = temp;
j += 1;
k += 1;
}
i += 1;
}
}
func right_to_top(_ matrix: inout[[Int]], _ mid: Int) {
var temp: Int = 0;
var i: Int = mid - 1;
var l: Int = mid + 1;
while (i >= 0) {
var j: Int = 0;
var k: Int = self.cols - 1;
while (j < mid && i - j >= 0 && k > mid && l + j < self.rows) {
temp = matrix[j][i - j];
matrix[j][i - j] = matrix[l + j][k];
matrix[l + j][k] = temp;
j += 1;
k -= 1;
}
i -= 1;
l += 1;
}
}
//Display matrix elements
func show_data(_ arr: [[Int]]) {
var i: Int = 0;
while (i < self.rows) {
var j: Int = 0;
while (j < self.cols) {
print(" ", arr[i][j], terminator: "");
j += 1;
}
print("\n", terminator: "");
i += 1;
}
print("\n", terminator: "");
}
func replace_corner(_ matrix: inout [[Int]]) {
if (self.rows != self.cols || self.cols % 2 == 0) {
print("\nNot a valid perfect Odd square matrix", terminator: "");
return;
}
let mid: Int = (self.cols) / 2;
self.left_to_bottom(&matrix, mid);
self.right_to_top(&matrix, mid);
}
}
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, 21],
[22, 23, 24, 25, 26, 27, 28],
[29, 30, 31, 32, 33, 34, 35],
[36, 37, 38, 39, 40, 41, 42],
[43, 44, 45, 46, 47, 48, 49]
];
let obj: MyMatrix = MyMatrix(matrix);
obj.show_data(matrix);
obj.replace_corner(&matrix);
obj.show_data(matrix);
}
main();
Output
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
49 42 35 4 29 36 43
48 41 10 11 12 37 44
47 16 17 18 19 20 45
22 23 24 25 26 27 28
5 30 31 32 33 34 3
6 13 38 39 40 9 2
7 14 21 46 15 8 1
Output Explanation
The given Java code creates a square matrix, calls the 'replace_corner' function to replace the corner elements, and then displays the original and modified matrices. The output displays the original and modified matrices as follows:
Original Matrix:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
Modified Matrix:
49 42 35 4 29 36 43
48 41 10 11 12 37 44
47 16 17 18 19 20 45
22 23 24 25 26 27 28
5 30 31 32 33 34 3
6 13 38 39 40 9 2
7 14 21 46 15 8 1
Time Complexity
The time complexity of the provided solution is O(N), where N is the total number of elements in the square matrix. The functions 'left_to_bottom' and 'right_to_top' traverse the corners of the matrix, and each element is swapped once. The swap operation takes constant time. Therefore, the overall time complexity is linear in the number of elements in the matrix.
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