Category: Matrix

Check if any row of the matrix can be converted to the elements present in the target row

  import java.util.*;  class GFG {                  public static boolean checkPossibleOrNot(        int[][] mat, int[] target)    {                  int[] comp = new int[mat[0].length];                          Arrays.fill(comp, Integer.MIN_VALUE);                  for (int[] val : mat) {                                                  if (val[0]

Continue Reading

Minimize count of steps to make all Matrix elements as 0 by replacing neighbours of K with 0

#include using namespace std;  void traverse(int M, int N,              vector& matrix,              int row, int col){          if (row >= 0 && row < M        && col >= 0 && col < N) {        if (matrix[row][col] == 0) {            return;        }                          matrix[row][col] = 0;                  traverse(M, N, matrix, row, col - 1);                  traverse(M, N, matrix, row, col + 1);                  traverse(M, N, matrix, row - 1, col);                  traverse(M, N, matrix, row + […]

Continue Reading

Minimum operations to convert Binary Matrix A to B by flipping submatrix of size K

#include using namespace std;  int minMoves(vector a,             vector b,             int K){        int n = a.size(), m = a[0].size();            int cntOperations = 0;          for (int i = 0; i < n; i++) {          for (int j = 0; j < m; j++) {                                        if (a[i][j] != b[i][j]) {                                                    cntOperations++;                                                                      if (i + K - 1 >= n                    || j + K – 1 >= m) {                    return -1;                }                                                  for […]

Continue Reading

Find the number of corner rectangles that can be formed from given Matrix

  #include using namespace std;  int countCornerRectangles(    vector& mat){        int count = 0;      int N = mat.size();    int M = mat[0].size();          map m;          for (int i = 0; i < N; i++) {                  for (int j = 0; j < M; j++) {            if (mat[i][j] == 1) {                                                  for (int k = j + 1;                     k < M; k++) {                    if (mat[i][k] == 1) {                        m[{ j, k }]++;                    }                }            }        }    }                  for […]

Continue Reading

Form a Rectangle from boundary elements of Matrix using Linked List

class Node:          def __init__(self, val):        self.data = val        self.next = None        self.prev = None        self.top = None        self.bottom = None    class LinkedList:          def __init__(self):        self.head = None              def Quad(self, grid, n, m):                  self.head = Node(grid[0][0])                  temp = self.head                  i = 0        j = 1                  while temp.top != self.head:                                                                                      if j < m and i == 0:                temp.next = Node(grid[i][j])                temp = temp.next                j += 1                                      elif j == m and i < n - 1:                i […]

Continue Reading

Find regions with most common region size in a given boolean matrix

#include using namespace std;#define ROW 4#define COL 5  int isSafe(int M[][COL], int row, int col,           bool visited[][COL]){    return (row >= 0) && (row < ROW) && (col >= 0)           && (col < COL)           && (M[row][col] && !visited[row][col]);}  void DFS(int M[][COL], int row, int col,         bool visited[][COL], int& count){            static int rowNbr[] = { -1, -1, -1, 0, 0, 1, 1, 1 };    static int colNbr[] = { -1, […]

Continue Reading

Rabbit House | Google Kickstart 2021 Round A

#include using namespace std;  void solve(long long int r, long long int c,           vector& grid){    priority_queue        pq;      for (long long int i = 0; i < r; i++) {        for (long long int j = 0; j < c; j++) {            pq.push(make_pair(grid[i][j],                              make_pair(i, j)));        }    }      long long int res = 0;      while (!pq.empty()) {        long long int height = pq.top().first,                      i = pq.top().second.first,                      j = pq.top().second.second;        pq.pop();        if (height != grid[i][j])            continue;        if […]

Continue Reading

Maximize matrix sum by flipping the sign of any adjacent pairs

Given a matrix mat[] of dimension N*N, the task is to find the maximum sum of matrix elements by flipping the signs of the adjacent element any number of times.Examples:Input: mat[][] = [[2, -2], [-2, 2]]OutputL: 8Explanation:Follow the steps below to find the maximum sum of matrix as:Flipping the sign of adjacent element (arr[0][0], arr[0][1]) […]

Continue Reading