Related Articles
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:

Input: A[][] = { {0, 0}, {0, 0}}, B[][] = {{1, 2}, {0, 1}}, X = 2
Output: Yes
Explanation:
Operation 1: Addinng 1 to A and A modifies A[][] to {{1, 1}, {0, 0}}.
Operation 2: Adding 1 to A and A 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 ``using` `namespace` `std;` `// Function to check whether Matrix A[][]``// can be transformed to Matrix B[][] or not``bool` `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``int` `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)) {``        ``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

 ``
Output:
`Yes`

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

My Personal Notes arrow_drop_up