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 :



title-img


                            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 Manipulation

Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array. Example: n=10 queries=[[1,5,3], [4,8,7], [6,9,1]] Queries are interpreted as follows: a b k 1 5 3 4 8 7 6 9 1 Add the valu

View Solution →

Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode

View Solution →

Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

View Solution →

Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

View Solution →

Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →

Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →