Open In App

Matrix rotation and corresponding zero elements

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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;
 
// Size of the Matrix.
const int m = 3;
 
int RotateMatrix(int A[][m], int B[][m])
{
 
    int C[m][m], status = 1;
 
    // Copying matrix A to matrix C.
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
            C[i][j] = A[i][j];
        }
    }
 
    // Now we know that the matrix can be
    // rotated maximum of 4 times.
    for (int r = 1; r <= 4; r++) {
        status = 1;
 
        // Applying the operation
        // on the matrix.
        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];
            }
        }
 
        // Checking for the required
        // condition.
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < m; j++) {
                if (A[i][j] == 0 and B[i][j] == 1) {
 
                    // If condition does
                    // not satisfy.
                    status = 0;
                    break;
                }
            }
            if (!status)
                break;
        }
 
        if (status) {
            cout << "Yes" << endl;
            return 0;
        }
    }
 
    cout << "No" << endl;
    return 0;
}
 
// Drivers code
int main()
{
 
    // Matrix A
    int A[m][m] = { { 1, 0, 0 }, { 0, 1, 1 }, { 1, 0, 1 } };
 
    // Matrix B
    int B[m][m] = { { 0, 0, 1 }, { 1, 1, 0 }, { 0, 0, 0 } };
 
    // Calling the function to rotate
    RotateMatrix(A, B);
 
    return 0;
}


Java




import java.util.Arrays;
 
// Nikunj Sonigara
class GFG {
    // Size of the Matrix.
    static final int m = 3;
 
    static void rotateMatrix(int[][] A, int[][] B) {
        int[][] C = new int[m][m];
        int status = 1;
 
        // Copying matrix A to matrix C.
        for (int i = 0; i < m; i++) {
            C[i] = Arrays.copyOf(A[i], m);
        }
 
        // Now we know that the matrix can be rotated maximum of 4 times.
        for (int r = 1; r <= 4; r++) {
            status = 1;
 
            // Applying the operation on the matrix.
            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);
            }
 
            // Checking for the required condition.
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < m; j++) {
                    if (A[i][j] == 0 && B[i][j] == 1) {
                        // If condition does not satisfy.
                        status = 0;
                        break;
                    }
                }
                if (status == 0) {
                    break;
                }
            }
 
            if (status == 1) {
                System.out.println("Yes");
                return;
            }
        }
 
        System.out.println("No");
    }
 
    // Drivers code
    public static void main(String[] args) {
        // Matrix A
        int[][] A = { { 1, 0, 0 }, { 0, 1, 1 }, { 1, 0, 1 } };
 
        // Matrix B
        int[][] B = { { 0, 0, 1 }, { 1, 1, 0 }, { 0, 0, 0 } };
 
        // Calling the function to rotate
        rotateMatrix(A, B);
    }
}


Python3




# Size of the Matrix
m = 3
 
def rotate_matrix(A, B):
    C = [[0] * m for _ in range(m)]
    status = 1
 
    # Copying matrix A to matrix C
    for i in range(m):
        for j in range(m):
            C[i][j] = A[i][j]
 
    # Now we know that the matrix can be rotated maximum of 4 times
    for r in range(1, 5):
        status = 1
 
        # Applying the operation on the matrix
        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]
 
        # Checking for the required condition
        for i in range(m):
            for j in range(m):
                if A[i][j] == 0 and B[i][j] == 1:
                    # If condition does not satisfy
                    status = 0
                    break
            if not status:
                break
 
        if status:
            print("Yes")
            return
 
    print("No")
 
# Driver code
def main():
    # Matrix A
    A = [[1, 0, 0], [0, 1, 1], [1, 0, 1]]
 
    # Matrix B
    B = [[0, 0, 1], [1, 1, 0], [0, 0, 0]]
 
    # Calling the function to rotate
    rotate_matrix(A, B)
 
main()


C#




using System;
 
public class GFG
{
    // Size of the Matrix.
    const int m = 3;
 
    static void RotateMatrix(int[,] A, int[,] B)
    {
        int[,] C = new int[m, m];
        int status = 1;
 
        // Copying matrix A to matrix C.
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < m; j++)
            {
                C[i, j] = A[i, j];
            }
        }
 
        // Now we know that the matrix can be
        // rotated maximum of 4 times.
        for (int r = 1; r <= 4; r++)
        {
            status = 1;
 
            // Applying the operation
            // on the matrix.
            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];
                }
            }
 
            // Checking for the required condition.
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (A[i, j] == 0 && B[i, j] == 1)
                    {
                        // If condition does not satisfy.
                        status = 0;
                        break;
                    }
                }
                if (status == 0)
                    break;
            }
 
            if (status == 1)
            {
                Console.WriteLine("Yes");
                return;
            }
        }
 
        Console.WriteLine("No");
    }
 
    // Drivers code
    static void Main(string[] args)
    {
        // Matrix A
        int[,] A = { { 1, 0, 0 }, { 0, 1, 1 }, { 1, 0, 1 } };
 
        // Matrix B
        int[,] B = { { 0, 0, 1 }, { 1, 1, 0 }, { 0, 0, 0 } };
 
        // Calling the function to rotate
        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;
    // Copying matrix A to
    // matrix C.
    for (let i = 0; i < m; i++) {
        for (let j = 0; j < m; j++) {
            C[i][j] = A[i][j];
        }
    }
    // Now we know that the matrix can be
    // the rotated a maximum of 4 times.
    for (let r = 1; r <= 4; r++) {
        status = 1;
        // Applying the operation on the matrix.
        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];
            }
        }
        // Checking for the required condition.
        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");
}
// Driver code
function main() {
    // Matrix A
    let A = [[1, 0, 0], [0, 1, 1], [1, 0, 1]];
    // Matrix B
    let B = [[0, 0, 1], [1, 1, 0], [0, 0, 0]];
    // Calling the function to rotate
    GFG(A, B);
}
main();


Output

Yes








Time Complexity: O(m2)
Auxiliary Space: O(m2)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads