forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMirrorOfMatrix.java
78 lines (65 loc) · 2.3 KB
/
MirrorOfMatrix.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.thealgorithms.matrix;
// Problem Statement
<<<<<<< HEAD
import com.thealgorithms.matrix.utils.MatrixUtil;
=======
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
/*
We have given an array of m x n (where m is the number of rows and n is the number of columns).
Print the new matrix in such a way that the new matrix is the mirror image of the original matrix.
The Original matrix is: | The Mirror matrix is:
1 2 3 | 3 2 1
4 5 6 | 6 5 4
7 8 9 | 9 8 7
@author - Aman (https://github.com/Aman28801)
*/
public final class MirrorOfMatrix {
private MirrorOfMatrix() {
}
<<<<<<< HEAD
public static double[][] mirrorMatrix(final double[][] originalMatrix) {
MatrixUtil.validateInputMatrix(originalMatrix);
=======
public static int[][] mirrorMatrix(final int[][] originalMatrix) {
if (originalMatrix == null) {
// Handle invalid input
return null;
}
if (originalMatrix.length == 0) {
return new int[0][0];
}
checkInput(originalMatrix);
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
int numRows = originalMatrix.length;
int numCols = originalMatrix[0].length;
<<<<<<< HEAD
double[][] mirroredMatrix = new double[numRows][numCols];
for (int i = 0; i < numRows; i++) {
mirroredMatrix[i] = MatrixUtil.reverseRow(originalMatrix[i]);
}
return mirroredMatrix;
}
=======
int[][] mirroredMatrix = new int[numRows][numCols];
for (int i = 0; i < numRows; i++) {
mirroredMatrix[i] = reverseRow(originalMatrix[i]);
}
return mirroredMatrix;
}
private static int[] reverseRow(final int[] inRow) {
int[] res = new int[inRow.length];
for (int i = 0; i < inRow.length; ++i) {
res[i] = inRow[inRow.length - 1 - i];
}
return res;
}
private static void checkInput(final int[][] matrix) {
// Check if all rows have the same number of columns
for (int i = 1; i < matrix.length; i++) {
if (matrix[i].length != matrix[0].length) {
throw new IllegalArgumentException("The input is not a matrix.");
}
}
}
>>>>>>> 754bf6c5f8f55b758bdee2667f6cadf4f0ab659f
}