Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Flipping the Matrix problem solution

YASH PAL, 31 July 202430 November 2025

In this HackerRank Flipping the Matrix problem solution you have 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

Foundation course by prepbytes

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)

{“mode”:”full”,”isActive”:false}

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);
        }
    }
}

{“mode”:”full”,”isActive”:false}

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;
}

{“mode”:”full”,”isActive”:false}

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;
}

{“mode”:”full”,”isActive”:false}

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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