Header Ad

HackerRank Flipping the Matrix problem solution

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("%d\n", 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("%d\n",sum);
    }

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

{"mode":"full","isActive":false}


Post a Comment

0 Comments