Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Check if a matrix can be converted to another by repeatedly adding any value to X consecutive elements in a row or column

  • Difficulty Level : Medium
  • Last Updated : 05 May, 2021

Given two matrices A[][] and B[][] of size M × N and an integer X, the task is to check if it is possible to convert matrix A[][] to matrix B[][] by adding any value to X consecutive cells in the same row or same column any number of times (possibly zero).

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: A[][] = { {0, 0}, {0, 0}}, B[][] = {{1, 2}, {0, 1}}, X = 2
Output: Yes
Explanation:
Operation 1: Addinng 1 to A[0][0] and A[0][1] modifies A[][] to {{1, 1}, {0, 0}}.
Operation 2: Adding 1 to A[0][1] and A[1][1] modifies A[][] to {{1, 2}, {0, 1}}.
After performing this two operations, matrix A[][] and B[][] are equal.



Input: A= {{0, 0, 0}, {0, 0, 0}}, B = {{1, 2, 3}, {4, 5, 6}}, X = 4
Output: False

 

Approach: The problem can be solved greedily by performing all the horizontal operations followed by the vertical operations. 
Follow the steps below to solve the problem:

  • Traverse the matrix to perform horizontal operations, using variables i and j over the ranges [0, M – 1] and [0, N – X], and perform the following operations:
    • If A[i][j] is not equal to B[i][j], increment next X elements in the same row by A[i][j] – B[i][j].
  • Now, traverse the matrix to perform vertical operations, using variables i and j over the ranges [0, M – X] and [0, N – 1] and perform the following operations:
    • Check if A[i][j] is equal to B[i][j] or not.
    • If found to be false, increment next X elements in the same column by A[i][j] – B[i][j].
  • Print “True” if matrices A[][] and B[][] are equal. Otherwise, print “False”.

Below is the implementation of the above approach:

C++




// C++ Program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to check whether Matrix A[][]
// can be transformed to Matrix B[][] or not
bool Check(int A[][2], int B[][2],
           int M, int N, int X)
{
    // Traverse the matrix to perform
    // horizontal operations
    for (int i = 0; i < M; i++) {
        for (int j = 0; j <= N - X; j++) {
 
            if (A[i][j] != B[i][j]) {
 
                // Calculate difference
                int diff = B[i][j] - A[i][j];
 
                for (int k = 0; k < X; k++) {
 
                    // Update next X elements
                    A[i][j + k] = A[i][j + k] + diff;
                }
            }
        }
    }
 
    // Traverse the matrix to perform
    // vertical operations
    for (int i = 0; i <= M - X; i++) {
        for (int j = 0; j < N; j++) {
 
            if (A[i][j] != B[i][j]) {
 
                // Calculate difference
                int diff = B[i][j] - A[i][j];
                for (int k = 0; k < X; k++) {
 
                    // Update next K elements
                    A[i + k][j] = A[i + k][j] + diff;
                }
            }
        }
    }
 
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
 
            // A[i][j] is not equal to B[i][j]
            if (A[i][j] != B[i][j]) {
 
                // Conversion is not possible
                return 0;
            }
        }
    }
 
    // Conversion is possible
    return 1;
}
 
// Driver Code
int main()
{
    // Input
    int M = 2, N = 2, X = 2;
    int A[2][2] = { { 0, 0 }, { 0, 0 } };
    int B[2][2] = { { 1, 2 }, { 0, 1 } };
 
    if (Check(A, B, M, N, X)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
 
    return 0;
}

Java




// Java program for the above approach
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to check whether Matrix A[][]
// can be transformed to Matrix B[][] or not
static int Check(int A[][], int B[][],
                 int M, int N, int X)
{
     
    // Traverse the matrix to perform
    // horizontal operations
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j <= N - X; j++)
        {
            if (A[i][j] != B[i][j])
            {
                 
                // Calculate difference
                int diff = B[i][j] - A[i][j];
 
                for(int k = 0; k < X; k++)
                {
                     
                    // Update next X elements
                    A[i][j + k] = A[i][j + k] + diff;
                }
            }
        }
    }
 
    // Traverse the matrix to perform
    // vertical operations
    for(int i = 0; i <= M - X; i++)
    {
        for(int j = 0; j < N; j++)
        {
            if (A[i][j] != B[i][j])
            {
                 
                // Calculate difference
                int diff = B[i][j] - A[i][j];
                for(int k = 0; k < X; k++)
                {
                     
                    // Update next K elements
                    A[i + k][j] = A[i + k][j] + diff;
                }
            }
        }
    }
 
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j < N; j++)
        {
             
            // A[i][j] is not equal to B[i][j]
            if (A[i][j] != B[i][j])
            {
                 
                // Conversion is not possible
                return 0;
            }
        }
    }
 
    // Conversion is possible
    return 1;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Input
    int M = 2, N = 2, X = 2;
    int A[][] = { { 0, 0 }, { 0, 0 } };
    int B[][] = { { 1, 2 }, { 0, 1 } };
 
    if (Check(A, B, M, N, X) != 0)
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by sanjoy_62

Python3




# Python3 program for the above approach
 
# Function to check whether Matrix A[][]
# can be transformed to Matrix B[][] or not
def Check(A, B, M, N, X):
     
    # Traverse the matrix to perform
    # horizontal operations
    for i in range(M):
        for j in range(N - X + 1):
            if (A[i][j] != B[i][j]):
 
                # Calculate difference
                diff = B[i][j] - A[i][j]
 
                for k in range(X):
                     
                    # Update next X elements
                    A[i][j + k] = A[i][j + k] + diff
 
    # Traverse the matrix to perform
    # vertical operations
    for i in range(M - X + 1):
        for j in range(N):
            if (A[i][j] != B[i][j]):
 
                # Calculate difference
                diff = B[i][j] - A[i][j]
                for k in range(X):
                     
                    # Update next K elements
                    A[i + k][j] = A[i + k][j] + diff
 
    for i in range(M):
        for j in range(N):
             
            # A[i][j] is not equal to B[i][j]
            if (A[i][j] != B[i][j]):
                 
                # Conversion is not possible
                return 0
 
    # Conversion is possible
    return 1
 
# Driver Code
if __name__ == '__main__':
     
    # Input
    M, N, X = 2, 2, 2
    A = [ [ 0, 0 ], [ 0, 0 ] ]
    B = [ [ 1, 2 ], [ 0, 1 ] ]
 
    if (Check(A, B, M, N, X)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by mohit kumar 29

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
  
// Function to check whether Matrix A[][]
// can be transformed to Matrix B[][] or not
static int Check(int [,]A, int [,]B,
                 int M, int N, int X)
{
     
    // Traverse the matrix to perform
    // horizontal operations
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j <= N - X; j++)
        {
            if (A[i, j] != B[i, j])
            {
                 
                // Calculate difference
                int diff = B[i, j] - A[i, j];
 
                for(int k = 0; k < X; k++)
                {
                     
                    // Update next X elements
                    A[i, j + k] = A[i, j + k] + diff;
                }
            }
        }
    }
 
    // Traverse the matrix to perform
    // vertical operations
    for(int i = 0; i <= M - X; i++)
    {
        for(int j = 0; j < N; j++)
        {
            if (A[i, j] != B[i, j])
            {
                 
                // Calculate difference
                int diff = B[i,j] - A[i,j];
                for(int k = 0; k < X; k++)
                {
                     
                    // Update next K elements
                    A[i + k, j] = A[i + k, j] + diff;
                }
            }
        }
    }
 
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j < N; j++)
        {
             
            // A[i][j] is not equal to B[i][j]
            if (A[i, j] != B[i, j])
            {
                 
                // Conversion is not possible
                return 0;
            }
        }
    }
 
    // Conversion is possible
    return 1;
}
 
// Driver Code
public static void Main()
{
     
    // Input
    int M = 2, N = 2, X = 2;
    int [,]A = { { 0, 0 }, { 0, 0 } };
    int [,]B = { { 1, 2 }, { 0, 1 } };
 
    if (Check(A, B, M, N, X) == 1)
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by SURENDRA_GANGWAR

Javascript




<script>
//Javascript program for the above approach
 
 
// Function to check whether Matrix A[][]
// can be transformed to Matrix B[][] or not
function Check( A, B,M, N, X)
{
    // Traverse the matrix to perform
    // horizontal operations
    for (var i = 0; i < M; i++) {
        for (var j = 0; j <= N - X; j++) {
 
            if (A[i][j] != B[i][j]) {
 
                // Calculate difference
                var diff = B[i][j] - A[i][j];
 
                for (var k = 0; k < X; k++) {
 
                    // Update next X elements
                    A[i][j + k] = A[i][j + k] + diff;
                }
            }
        }
    }
 
    // Traverse the matrix to perform
    // vertical operations
    for (var i = 0; i <= M - X; i++) {
        for (var j = 0; j < N; j++) {
 
            if (A[i][j] != B[i][j]) {
 
                // Calculate difference
                var diff = B[i][j] - A[i][j];
                for (var k = 0; k < X; k++) {
 
                    // Update next K elements
                    A[i + k][j] = A[i + k][j] + diff;
                }
            }
        }
    }
 
    for (var i = 0; i < M; i++) {
        for (var j = 0; j < N; j++) {
 
            // A[i][j] is not equal to B[i][j]
            if (A[i][j] != B[i][j]) {
 
                // Conversion is not possible
                return 0;
            }
        }
    }
 
    // Conversion is possible
    return 1;
}
 
var M = 2, N = 2, X = 2;
    var A = [ [ 0, 0 ], [ 0, 0 ]];
    var B = [ [ 1, 2 ], [ 0, 1 ] ];
 
    if (Check(A, B, M, N, X)) {
       document.write( "Yes" + "<br>");
    }
    else {
        document.write( "No" + "<br>");
    }
 
//This code is contributed by SoumikMondal
</script>
Output: 
Yes

 

Time Complexity: O(M * N * X)
Auxiliary Space: O(1)

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :