Skip to content

Commit dd3610d

Browse files
committed
Matrix: flippingTheArray add source + style change
1 parent 268e34d commit dd3610d

File tree

1 file changed

+14
-12
lines changed
  • src/algorithm_practice/Array_Algorithms/flippingTheMatrix

1 file changed

+14
-12
lines changed

src/algorithm_practice/Array_Algorithms/flippingTheMatrix/mat.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#include <iostream>
55
#include <algorithm>
66

7-
// Source: https://www.hackerrank.com/contests/world-codesprint-6/challenges/flipping-the-matrix
7+
// Source:
8+
// - https://www.hackerrank.com/contests/world-codesprint-6/challenges/flipping-the-matrix
9+
// - https://www.hackerrank.com/challenges/flipping-the-matrix
810

911
/**
1012
* Flipping the matrix
@@ -24,11 +26,11 @@
2426
* Space complexity: O(1)
2527
*/
2628

27-
void initMatrix(std::vector<std::vector<int> >& inputMatrix, int n) {
29+
void initMatrix(std::vector<std::vector<int>>& inputMatrix, int n) {
2830
inputMatrix.clear();
29-
inputMatrix.resize(2*n, std::vector<int>(2*n));
30-
for (int i = 0; i < 2*n; ++i) {
31-
for (int j = 0; j < 2*n; ++j) {
31+
inputMatrix.resize(n * 2, std::vector<int>(n * 2));
32+
for (int i = 0; i < n * 2; ++i) {
33+
for (int j = 0; j < n * 2; ++j) {
3234
std::cin >> inputMatrix[i][j];
3335
}
3436
}
@@ -38,16 +40,16 @@ int max(int m1, int m2, int m3, int m4) {
3840
return std::max(m4, std::max(m3, std::max(m1, m2)));
3941
}
4042

41-
long maxSum(std::vector<std::vector<int> >& matrix) {
43+
long maxSum(std::vector<std::vector<int>>& matrix) {
4244
long returnMaxSum = 0;
4345
int topLeft, topRight, bottomLeft, bottomRight;
4446

45-
for (int i = 0; i < matrix.size()/2; ++i) {
46-
for (int j = 0; j < matrix.size()/2; ++j) {
47+
for (int i = 0; i < matrix.size() / 2; ++i) {
48+
for (int j = 0; j < matrix.size() / 2; ++j) {
4749
topLeft = matrix[i][j];
48-
topRight = matrix[i][matrix.size()-1-j];
49-
bottomLeft = matrix[matrix.size()-1-i][j];
50-
bottomRight = matrix[matrix.size()-1-i][matrix.size()-1-j];
50+
topRight = matrix[i][matrix.size() - 1 - j];
51+
bottomLeft = matrix[matrix.size() - 1 - i][j];
52+
bottomRight = matrix[matrix.size() - 1 - i][matrix.size() - 1 - j];
5153
returnMaxSum += max(topLeft, topRight, bottomLeft, bottomRight);
5254
}
5355
}
@@ -58,7 +60,7 @@ long maxSum(std::vector<std::vector<int> >& matrix) {
5860
int main() {
5961
int q, n;
6062
std::cin >> q;
61-
std::vector<std::vector<int> > matrix;
63+
std::vector<std::vector<int>> matrix;
6264

6365
for (int i = 0; i < q; ++i) {
6466
std::cin >> n;

0 commit comments

Comments
 (0)