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
Array Pairs
Consider an array of n integers, A = [ a1, a2, . . . . an] . Find and print the total number of (i , j) pairs such that ai * aj <= max(ai, ai+1, . . . aj) where i < j. Input Format The first line contains an integer, n , denoting the number of elements in the array. The second line consists of n space-separated integers describing the respective values of a1, a2 , . . . an .
View Solution →Self Balancing Tree
An AVL tree (Georgy Adelson-Velsky and Landis' tree, named after the inventors) is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. We define balance factor for each node as : balanceFactor = height(left subtree) - height(righ
View Solution →Array and simple queries
Given two numbers N and M. N indicates the number of elements in the array A[](1-indexed) and M indicates number of queries. You need to perform two types of queries on the array A[] . You are given queries. Queries can be of two types, type 1 and type 2. Type 1 queries are represented as 1 i j : Modify the given array by removing elements from i to j and adding them to the front. Ty
View Solution →Median Updates
The median M of numbers is defined as the middle number after sorting them in order if M is odd. Or it is the average of the middle two numbers if M is even. You start with an empty number list. Then, you can add numbers to the list, or remove existing numbers from it. After each add or remove operation, output the median. Input: The first line is an integer, N , that indicates the number o
View Solution →Maximum Element
You have an empty sequence, and you will be given N queries. Each query is one of these three types: 1 x -Push the element x into the stack. 2 -Delete the element present at the top of the stack. 3 -Print the maximum element in the stack. Input Format The first line of input contains an integer, N . The next N lines each contain an above mentioned query. (It is guaranteed that each
View Solution →Balanced Brackets
A bracket is considered to be any one of the following characters: (, ), {, }, [, or ]. Two brackets are considered to be a matched pair if the an opening bracket (i.e., (, [, or {) occurs to the left of a closing bracket (i.e., ), ], or }) of the exact same type. There are three types of matched pairs of brackets: [], {}, and (). A matching pair of brackets is not balanced if the set of bra
View Solution →