Matrix rotation and corresponding zero elements
Last Updated :
25 Sep, 2023
Given two square matrices A and B of size m, where each element can either be 1 or 0, the task is to determine if the matrix A can be rotated any number of times in such a way that for every A[i][j] = 0, there exists a B[i][j]=0 in matrix B. The rotation operation replaces A[i][j] with A[m-j-1][i] for every pair of integers (i, j) where 0 ≤ i, j < m.
Examples:
Input: m = 3, A = {{1, 0, 0}, {0, 1, 1}, {1, 0, 1}}, B = {{0, 0, 1}, {1, 1, 0}, {0, 0, 0}}
Output: Yes
Explanation: After 0 or more rotation of A.
- At the beginning A is:
1 0 0
0 1 1
1 0 1
- After rotating A once, the A will be:
1 0 1
0 1 0
1 1 0
Here we can check that for every A[i][j] = 0, B[i][j] is not equal to 0.
- Rotating A once again:
1 0 1
1 1 0
0 0 1
Now we can see that for every A[i][j] = 0, B[i][j] is equal to 0.
Input: m = 5, A = {{1, 1, 0, 0, 1}, {0, 1, 1, 0, 1}, {1, 1, 0, 1, 0}, {1, 0, 1, 0, 1}, {1, 0, 1, 1, 0}, B = {{0, 0, 1, 1, 0}, {1, 0, 0, 0, 1}, {1, 1, 0, 0, 0}, {0, 1, 0, 1, 0}, {0, 0, 1, 0, 1}}
Output: No
Approach: To solve the problem follow the below idea:
We just have to perform the above-mentioned operation which is to replace A[i][j] with A[m-j-1][i]. We can notice that after the fourth rotation or after rotating the Matrix A four times the initial and the final state of the Matrix A will be the same.
Steps were taken to solve the Problem:
- First, replace every A[i][j] element with the A[m-j-1][i] element in the matrix A.
- Then traverse the matrix A and check if for every A[i][j]=0, B[i][j] is also 0 or not.
- If for every A[i][j]=0 every B[i][j]!=0 then repeat step-1 followed by step-2.
- If even after repeating step-1 four times our condition does not satisfy; that is for every A[i][j] = 0, B[i][j] = 0, then print No, else print Yes.
Below is the implementation for the above approach:
C++
#include <iostream>
using namespace std;
const int m = 3;
int RotateMatrix( int A[][m], int B[][m])
{
int C[m][m], status = 1;
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < m; j++) {
C[i][j] = A[i][j];
}
}
for ( int r = 1; r <= 4; r++) {
status = 1;
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < m; j++) {
C[m - j - 1][i] = A[i][j];
}
}
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < m; j++) {
A[i][j] = C[i][j];
}
}
for ( int i = 0; i < m; i++) {
for ( int j = 0; j < m; j++) {
if (A[i][j] == 0 and B[i][j] == 1) {
status = 0;
break ;
}
}
if (!status)
break ;
}
if (status) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}
int main()
{
int A[m][m] = { { 1, 0, 0 }, { 0, 1, 1 }, { 1, 0, 1 } };
int B[m][m] = { { 0, 0, 1 }, { 1, 1, 0 }, { 0, 0, 0 } };
RotateMatrix(A, B);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static final int m = 3 ;
static void rotateMatrix( int [][] A, int [][] B) {
int [][] C = new int [m][m];
int status = 1 ;
for ( int i = 0 ; i < m; i++) {
C[i] = Arrays.copyOf(A[i], m);
}
for ( int r = 1 ; r <= 4 ; r++) {
status = 1 ;
for ( int i = 0 ; i < m; i++) {
for ( int j = 0 ; j < m; j++) {
C[m - j - 1 ][i] = A[i][j];
}
}
for ( int i = 0 ; i < m; i++) {
A[i] = Arrays.copyOf(C[i], m);
}
for ( int i = 0 ; i < m; i++) {
for ( int j = 0 ; j < m; j++) {
if (A[i][j] == 0 && B[i][j] == 1 ) {
status = 0 ;
break ;
}
}
if (status == 0 ) {
break ;
}
}
if (status == 1 ) {
System.out.println( "Yes" );
return ;
}
}
System.out.println( "No" );
}
public static void main(String[] args) {
int [][] A = { { 1 , 0 , 0 }, { 0 , 1 , 1 }, { 1 , 0 , 1 } };
int [][] B = { { 0 , 0 , 1 }, { 1 , 1 , 0 }, { 0 , 0 , 0 } };
rotateMatrix(A, B);
}
}
|
Python3
m = 3
def rotate_matrix(A, B):
C = [[ 0 ] * m for _ in range (m)]
status = 1
for i in range (m):
for j in range (m):
C[i][j] = A[i][j]
for r in range ( 1 , 5 ):
status = 1
for i in range (m):
for j in range (m):
C[m - j - 1 ][i] = A[i][j]
for i in range (m):
for j in range (m):
A[i][j] = C[i][j]
for i in range (m):
for j in range (m):
if A[i][j] = = 0 and B[i][j] = = 1 :
status = 0
break
if not status:
break
if status:
print ( "Yes" )
return
print ( "No" )
def main():
A = [[ 1 , 0 , 0 ], [ 0 , 1 , 1 ], [ 1 , 0 , 1 ]]
B = [[ 0 , 0 , 1 ], [ 1 , 1 , 0 ], [ 0 , 0 , 0 ]]
rotate_matrix(A, B)
main()
|
C#
using System;
public class GFG
{
const int m = 3;
static void RotateMatrix( int [,] A, int [,] B)
{
int [,] C = new int [m, m];
int status = 1;
for ( int i = 0; i < m; i++)
{
for ( int j = 0; j < m; j++)
{
C[i, j] = A[i, j];
}
}
for ( int r = 1; r <= 4; r++)
{
status = 1;
for ( int i = 0; i < m; i++)
{
for ( int j = 0; j < m; j++)
{
C[m - j - 1, i] = A[i, j];
}
}
for ( int i = 0; i < m; i++)
{
for ( int j = 0; j < m; j++)
{
A[i, j] = C[i, j];
}
}
for ( int i = 0; i < m; i++)
{
for ( int j = 0; j < m; j++)
{
if (A[i, j] == 0 && B[i, j] == 1)
{
status = 0;
break ;
}
}
if (status == 0)
break ;
}
if (status == 1)
{
Console.WriteLine( "Yes" );
return ;
}
}
Console.WriteLine( "No" );
}
static void Main( string [] args)
{
int [,] A = { { 1, 0, 0 }, { 0, 1, 1 }, { 1, 0, 1 } };
int [,] B = { { 0, 0, 1 }, { 1, 1, 0 }, { 0, 0, 0 } };
RotateMatrix(A, B);
}
}
|
Javascript
const m = 3;
function GFG(A, B) {
let C = new Array(m).fill(0).map(() => new Array(m).fill(0));
let status = 1;
for (let i = 0; i < m; i++) {
for (let j = 0; j < m; j++) {
C[i][j] = A[i][j];
}
}
for (let r = 1; r <= 4; r++) {
status = 1;
for (let i = 0; i < m; i++) {
for (let j = 0; j < m; j++) {
C[m - j - 1][i] = A[i][j];
}
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < m; j++) {
A[i][j] = C[i][j];
}
}
for (let i = 0; i < m; i++) {
for (let j = 0; j < m; j++) {
if (A[i][j] === 0 && B[i][j] === 1) {
status = 0;
break ;
}
}
if (!status) {
break ;
}
}
if (status) {
console.log( "Yes" );
return ;
}
}
console.log( "No" );
}
function main() {
let A = [[1, 0, 0], [0, 1, 1], [1, 0, 1]];
let B = [[0, 0, 1], [1, 1, 0], [0, 0, 0]];
GFG(A, B);
}
main();
|
Time Complexity: O(m2)
Auxiliary Space: O(m2)
Share your thoughts in the comments
Please Login to comment...