Flipping the Matrix HackerRank Optimised Solution in C++, Java, Python with Explanation

Subhanu
3 min readJul 31, 2023

--

Flipping Matrix HackerRank Solution

Difficulty: Medium

Full Problem Description : Flipping the Matrix Problem Description

Problem Description :

Sean invented a game involving a 2n * 2n matrix where each cell of the matrix contains an integer. He can reverse any of its rows or columns any number of times. The goal of the game is to maximize the sum of the elements in the n *n submatrix located in the upper-left quadrant of the matrix.

Given the initial configurations for q matrices, help Sean reverse the rows and columns of each matrix in the best possible way so that the sum of the elements in the matrix’s upper-left quadrant is maximal.

# Input :

matrix = [[1, 2], [3, 4]]

# Output :

4

# Input :

matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]]

# Output :

119 + 114 + 56 + 125 = 414

Here we can find solution using following pattern,

Maximise the indexes which have same color

So simply we have to find Max of same number of box like (1,1,1,1). And last return Sum of all Max numbers.

We have to return Sum of Top-Left Matrix (1, 2, 3, 4), But before that we have to find Max number from Matrix.

C++ Code:

int flippingMatrix(vector<vector<int>> matrix) {
int sum=0;
int n=matrix.size();

for(int i=0;i<n/2;i++)
{
for(int j=0;j<n/2;j++)
{
sum+=
max(matrix[i][j],
(max(matrix[i][n-1-j],
(max(matrix[n-1-i][j],matrix[n-1-i][n-1-j]))
)
)
);
}
}
return sum;
}

Java Code:

import java.util.List;

public class MatrixFlipping {
public int flippingMatrix(List<List<Integer>> matrix) {
int sum = 0;
int n = matrix.size();

for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < n / 2; j++) {
sum += Math.max(matrix.get(i).get(j),
Math.max(matrix.get(i).get(n - 1 - j),
Math.max(matrix.get(n - 1 - i).get(j), matrix.get(n - 1 - i).get(n - 1 - j))));
}
}
return sum;
}
}

Python Code:

def flipping_matrix(matrix):
sum = 0
n = len(matrix)

for i in range(n // 2):
for j in range(n // 2):
sum += max(matrix[i][j],
max(matrix[i][n - 1 - j],
max(matrix[n - 1 - i][j], matrix[n - 1 - i][n - 1 - j])))

return sum

Solution Explanation :

Let’s take the example and see,

matrix = [[112, 42, 83, 119], [56, 125, 56, 49], [15, 78, 101, 43], [62, 98, 114, 108]]

Example

We have to return Sum of all Max numbers that have same colors. like first we have to find Max from [112, 119, 62, 108] and so on.

  • We are looping through only half of list because we have to find Sum of Top-Left corner of matrix.
  • As traverse through given List of List we are getting following answer in sum variable.
  1. sum += Max [112, 119, 62, 108] | sum = 0 + 119
  2. sum += Max [42, 83, 98, 114] | sum = 119 + 114 = 233
  3. sum += Max [56, 49, 15, 43] | sum = 233 + 56 = 289
  4. sum += Max [125, 56, 78, 101] | sum = 289 + 125 = 414
  • return sum

The sum returned will be the maximal sum obtained for the first quadrant.

If you have any questions or would like to request detailed solutions on other problems, please leave a comment below. Your feedback is valuable, and I am always eager to provide content that meets your needs.

Thank you for reading, and happy coding!

Cheers 🥂

--

--

Subhanu

🚀 22yr old software dev, building SaaS projects full time. I teach students, software development as a hobby and I am learning Spanish . Mucho gusto 💁‍♂️