Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Flipping the Matrix problem solution

YASH PAL, 31 July 202425 January 2026

In this HackerRank Flipping the Matrix problem solution, Sean invented a game involving a 2n x 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 x 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.

HackerRank Flipping the Matrix problem solution

HackerRank Flipping the Matrix problem solution in Python.

import math
q = int(input().strip())

for a0 in range(q):
    n = int(input().strip())
    a =[[0 for j in range(2*n)] for i in range(2*n)]
    
    for x in range(2 * n):
        c = [int(c_temp) for c_temp in input().strip().split(' ')]        
        a[x] = c
     
    
    v = 0
    for i in range(n):
        for j in range(n):
            l = []
            l.append(a[i][j]) # current matrix
            l.append(a[2 * n - 1 - i][j])  # bottom left
            l.append(a[i][2 * n - 1- j]) # top right
            l.append(a[2* n - 1 - i][2 * n - 1- j]) # bottom right
    
            maxv = max(l)
            #print(l)
            #print(max(l))
            
            v += maxv

    print(v)

Flipping the Matrix problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int queries = in.nextInt();
int size = 0;
int length;
int total;
for(int i = 0; i < queries; i++){
total = 0;
size = in.nextInt();
length = size * 2;
int[][] matrix = new int[length][length];
for(int row = 0; row < length; row++){
for(int column = 0; column < length; column++){
matrix[column][row] = in.nextInt();
}
}
int max = 0;
for(int row = 0; row < size; row++){
for(int column = 0; column < size; column++){
max = Integer.MIN_VALUE;
if(matrix[row][column] > max)
max = matrix[row][column];
if(matrix[row][length - column - 1] > max)
max = matrix[row][length - column - 1];
if(matrix[length - row - 1][column] > max)
max = matrix[length - row - 1][column];
if(matrix[length - row - 1][length - column - 1] > max)
max = matrix[length - row - 1][length - column - 1];
total += max;
}
}
System.out.println(total);
}
}
}

Problem solution in C++.

#include <cstdio>
#include <cassert>
#include <algorithm>
using namespace std;

const int MAXN = 300;

int matrix[MAXN][MAXN];

void Work() {
  int n;
  assert(scanf("%d", &n) == 1);
  int halfn = n;
  n *= 2;

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      assert(scanf("%d", &matrix[i][j]) == 1);
    }
  }

  int ans = 0;
  for (int i = 0; i < halfn; ++i) {
    for (int j = 0; j < halfn; ++j) {
      int t_max = matrix[i][j];
      int ti = i;
      int tj = j;

      ti = n - 1 - ti;
      t_max = max(t_max, matrix[ti][tj]);

      tj = n - 1 - tj;
      t_max = max(t_max, matrix[ti][tj]);

      ti = n - 1 - ti;
      t_max = max(t_max, matrix[ti][tj]);

      ans += t_max;
    }
  }
  printf("%dn", ans);
}

int main() {
  int q;
  assert(scanf("%d", &q) == 1);
  while (q--) {
    Work();
  }
  return 0;
}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int maxm(int a,int b,int c,int d){
    int max;
    if(a>=b && a>=c && a>=d){
        max=a;
    }
    else if(a<=b && b>=c && b>=d)
        max=b;
    else if(c>=b && a<=c && c>=d)
        max=c;
   else
        max=d;
   return max;
        }
int main() {
    int q;
    scanf("%d",&q);
    while(q--){
        int n,i,j;
        scanf("%d",&n);
        int x[2*n][2*n];
        for(i=0;i<2*n;i++){
            for(j=0;j<2*n;j++)
                scanf("%d",&x[i][j]);
        }
        int m=2*n;
        int sum=0;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                sum+=maxm(x[i][j],x[i][m-1-j],x[m-1-i][j],x[m-1-i][m-1-j]);
            }
        }
        printf("%dn",sum);
    }

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    return 0;
}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes