**Flipping the Matrix**

### Problem Statement :

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. Example matrix = [[1,2],[3,4]] 1 2 3 4 It is 2*2 and we want to maximize the top left quadrant, a 1*1 matrix. Reverse row 1: 1 2 4 3 And now reverse column 0: 4 2 1 3 The maximal sum is 4. Function Description Complete the flippingMatrix function in the editor below. flippingMatrix has the following parameters: - int matrix[2n][2n]: a 2-dimensional array of integers Returns - int: the maximum sum possible. Input Format The first line contains an integer q, the number of queries. The next q sets of lines are in the following format: The first line of each query contains an integer, n. Each of the next 2n lines contains 2n space-separated integers matrix[i][j] in row i of the matrix. Constraints 1 <= q <= 16 1 <= n <= 128 0 <= matrix[i][j] <= 4096, where 0 <= i,j < 2n.

### Solution :

` ````
Solution in C :
In C++ :
#include <bits/stdc++.h>
#define FI(i,a,b) for(int i=(a);i<=(b);i++)
#define FD(i,a,b) for(int i=(a);i>=(b);i--)
#define LL long long
#define Ldouble long double
#define PI 3.1415926535897932384626
#define PII pair<int,int>
#define PLL pair<LL,LL>
#define mp make_pair
#define fi first
#define se second
using namespace std;
int q, n, s[299][299];
int main(){
scanf("%d", &q);
while(q--){
scanf("%d", &n);
FI(i, 1, 2 * n) FI(j, 1, 2 * n) scanf("%d", &s[i][j]);
int ans = 0;
FI(i, 1, n) FI(j, 1, n){
int i2 = n + n + 1 - i;
int j2 = n + n + 1 - j;
int mx = max(max(max(s[i][j], s[i][j2]), s[i2][j]), s[i2][j2]);
ans += mx;
}
printf("%d\n", ans);
}
return 0;
}
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args)throws IOException {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(in.readLine());
for(int t1=0;t1<t;t1++){
int n=Integer.parseInt(in.readLine());
String[][] s=new String[2*n][2*n];
for(int i=0;i<2*n;i++)
s[i]=in.readLine().split(" ");
long sum=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
sum+=Math.max(Math.max(Integer.parseInt(s[i][j]),
Integer.parseInt(s[2*n-1-i][j])),
Math.max(Integer.parseInt(s[i][2*n-1-j]),
Integer.parseInt(s[2*n-1-i][2*n-1-j])));
}
}
System.out.println(sum);
}
}
}
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int q;
scanf("%d",&q);
while(q--){
int n;
scanf("%d",&n);
int matrix[2*n][2*n];
int sol[2*n][2*n];
for(int i=0;i<2*n;i++){
for(int j=0;j<2*n;j++){
scanf("%d",&matrix[i][j]);
sol[i][j]=0;
}
}
int sum=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int a = matrix[i][j];
if(matrix[2*n-1-i][2*n-1-j]>a && sol[2*n-1-i][2*n-1-j]==0){
a=matrix[2*n-1-i][2*n-1-j];
sol[2*n-1-i][2*n-1-j]++;
}
if(matrix[i][2*n-1-j]>a && sol[i][2*n-1-j]==0){
a=matrix[i][2*n-1-j];
sol[i][2*n-1-j]++;
}
if(matrix[2*n-1-i][j]>a && sol[2*n-1-i][j]==0){
a=matrix[2*n-1-i][j];
sol[2*n-1-i][j]++;
}
sum+=a;
}
}
printf("%d\n",sum);
}
return 0;
}
In Python3 :
q = int(input())
for _ in range(q):
n = int(input())
a = []
for y in range(2*n):
a.append([int(x) for x in input().split()])
suma = 0
for i in range(n):
for j in range(n):
suma += max(max(a[i][j],a[2*n-i-1][j]),max(a[i][2*n-j-1],a[2*n-i-1][2*n-j-1]))
print(suma)
```

## View More Similar Problems

## Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

View Solution →## The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

View Solution →## Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

View Solution →## Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

View Solution →## Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T

View Solution →## Pair Sums

Given an array, we define its value to be the value obtained by following these instructions: Write down all pairs of numbers from this array. Compute the product of each pair. Find the sum of all the products. For example, for a given array, for a given array [7,2 ,-1 ,2 ] Note that ( 7 , 2 ) is listed twice, one for each occurrence of 2. Given an array of integers, find the largest v

View Solution →