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

• Last Updated : 23 May, 2022

Given a matrix mat[][] of dimensions N*M and an array target[] of M integers, the task is to check whether any row of the matrix can be made equal to the array target[] by choosing any two rows of the matrix and update each element of any of the row to the maximum of elements at corresponding indices of the two chosen row. If it is possible to do so, then print Yes. Otherwise, print No.

Examples:

Input: mat[][] = {{2, 5, 3}, {2, 8, 4}, {1, 5, 7}}, target[] = {2, 5, 7}
Output: Yes
Explanation:
After perform the given operation on row 3 and row 1 and modifying the row-3 becomes:
[max(1, 2), max(5, 5), max(7, 3)] = [2, 5, 7] which is equal to target

Input: mat[][] = {{3, 4, 5}, {4, 5, 6}}, target[] = {3, 2, 5}
Output: No

Approach: The given problem can be solved by using the Greedy Approach, the idea is to create an auxiliary array comp[] and iterate through every row of the matrix and if the rows have elements less than or equal to corresponding elements in the target array then perform Math.max() operation on every element of the row and comp. Below steps can be followed:

• Initialize an array, say comp[] of size M with every element equal to INT_MIN.
• Traverse the matrix mat[][] and for every row check if all of the values in that row are less than or equal to corresponding values in the target array or not. If found to be true, then update the comp[] array by taking the maximum of every element present in comp[] with the corresponding element in the row.
• After the above steps, if the array comp[] is the same as the array target[] then print Yes. Otherwise, print No.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to check if target array``// can be formed from any row in trio``// with the given operations``int` `checkPossibleOrNot(vector >& mat,``                       ``vector<``int``>& target)``{` `    ``// Initialize comp[] of size M``    ``vector<``int``> comp(mat[0].size(), INT_MIN);` `    ``// Traverse the matrix mat[]``    ``for` `(``auto` `val : mat) {` `        ``// Check if all values in a row``        ``// is at most the corresponding``        ``// values in target[] array``        ``if` `(val[0] <= target[0] && val[1] <= target[1]``            ``&& val[2] <= target[2])` `            ``// Update the comp[]``            ``comp = { max(comp[0], val[0]),``                     ``max(comp[1], val[1]),``                     ``max(comp[2], val[2]) };``    ``}` `    ``// Return the possibility``    ``return` `(comp == target);``}` `// Driver Code``int` `main()``{``    ``vector > mat``        ``= { { 2, 5, 3 }, { 2, 8, 4 }, { 1, 5, 7 } };``    ``vector<``int``> target = { 2, 5, 7 };` `    ``string ans``        ``= checkPossibleOrNot(mat, target) ? ``"Yes"` `: ``"NO"``;``    ``cout << ans;` `    ``return` `0;``}` `    ``// This code is contributed by rakeshsahni`

## Java

 `// Java program for the above approach` `import` `java.util.*;` `class` `GFG {` `    ``// Function to check if target array``    ``// can be formed from any row in trio``    ``// with the given operations``    ``public` `static` `boolean` `checkPossibleOrNot(``        ``int``[][] mat, ``int``[] target)``    ``{` `        ``// Initialize comp[] of size M``        ``int``[] comp = ``new` `int``[mat[``0``].length];` `        ``// Fill the array with minimum``        ``// value of integer``        ``Arrays.fill(comp, Integer.MIN_VALUE);` `        ``// Traverse the matrix mat[]``        ``for` `(``int``[] val : mat) {` `            ``// Check if all values in a row``            ``// is at most the corresponding``            ``// values in target[] array``            ``if` `(val[``0``] <= target[``0``]``                ``&& val[``1``] <= target[``1``]``                ``&& val[``2``] <= target[``2``])` `                ``// Update the comp[]``                ``comp = ``new` `int``[] { Math.max(``                                       ``comp[``0``], val[``0``]),``                                   ``Math.max(``                                       ``comp[``1``], val[``1``]),``                                   ``Math.max(``                                       ``comp[``2``], val[``2``]) };``        ``}` `        ``// Return the possibility``        ``return` `Arrays.equals(comp, target);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int``[][] mat``            ``= { { ``2``, ``5``, ``3` `}, { ``2``, ``8``, ``4` `}, { ``1``, ``5``, ``7` `} };``        ``int``[] target = { ``2``, ``5``, ``7` `};` `        ``String ans = checkPossibleOrNot(``                         ``mat, target)``                         ``? ``"Yes"``                         ``: ``"NO"``;``        ``System.out.println(ans);``    ``}``}`

## Python3

 `# Python3 program for the above approach``import` `sys` `# Function to check if target array``# can be formed from any row in trio``# with the given operations``def` `checkPossibleOrNot(mat, target) :` `    ``comp ``=` `[] ;``    ``INT_MIN ``=` `-` `(sys.maxsize ``-` `1``)``    ` `    ``# Initialize comp[] of size M``    ``for` `i ``in` `range``(``len``(mat[``0``])) :``        ``comp.append(INT_MIN)` `    ``# Traverse the matrix mat[]``    ``for` `val ``in` `mat :` `        ``# Check if all values in a row``        ``# is at most the corresponding``        ``# values in target[] array``        ``if` `(val[``0``] <``=` `target[``0``] ``and` `val[``1``] <``=` `target[``1``] ``and` `val[``2``] <``=` `target[``2``]) :` `            ``# Update the comp[]``            ``comp ``=` `[ ``max``(comp[``0``], val[``0``]), ``max``(comp[``1``], val[``1``]), ``max``(comp[``2``], val[``2``]) ];``    ` `    ``if` `comp ``=``=` `target :``        ` `        ``# Return the possibility``        ``return` `True``    ``else` `:``        ` `        ``# Return the possibility``        ``return` `False` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:``    ` `    ``mat ``=` `[ [ ``2``, ``5``, ``3` `], [ ``2``, ``8``, ``4` `], [ ``1``, ``5``, ``7` `] ];``    ``target ``=` `[ ``2``, ``5``, ``7` `];` `    ``ans ``=` `checkPossibleOrNot(mat, target)``    ` `    ``if` `ans:``        ``print``(``"Yes"``)``    ``else` `:``        ``print``(``"NO"``)` `    ``# This code is contributed by AnkThon`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Linq;` `public` `class` `GFG {` `    ``// Function to check if target array``    ``// can be formed from any row in trio``    ``// with the given operations``    ``public` `static` `bool` `checkPossibleOrNot(``        ``int` `[,]mat, ``int``[] target)``    ``{` `        ``// Initialize comp[] of size M``        ``int``[] comp = ``new` `int``[mat.GetLength(0)];` `        ``// Fill the array with minimum``        ``// value of integer``        ``for` `(``int` `i = 0; i < mat.GetLength(0); i++)``            ``comp[i] = ``int``.MinValue;``            ` `        ``// Traverse the matrix mat[]``        ``for` `(``int` `i = 0; i < mat.GetLength(0); i++){``            ` `            ``int` `[]val = {mat[i, 0], mat[i, 1], mat[i, 2]};` `            ``// Check if all values in a row``            ``// is at most the corresponding``            ``// values in target[] array``            ``if` `(val[0] <= target[0]``                ``&& val[1] <= target[1]``                ``&& val[2] <= target[2])` `                ``// Update the comp[]``                ``comp = ``new` `int``[] { Math.Max(``                                    ``comp[0], val[0]),``                                ``Math.Max(``                                    ``comp[1], val[1]),``                                ``Math.Max(``                                    ``comp[2], val[2]) };``        ``}` `        ``// Return the possibility``        ``return` `comp.SequenceEqual(target);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(``string``[] args)``    ``{``        ``int``[,] mat = { { 2, 5, 3 }, { 2, 8, 4 }, { 1, 5, 7 } };``        ``int``[] target = { 2, 5, 7 };` `        ``string` `ans = checkPossibleOrNot(``                        ``mat, target)``                        ``? ``"Yes"``                        ``: ``"NO"``;``        ``Console.WriteLine(ans);``    ``}``}` `// This code is contributed by AnkThon`

## Javascript

 ``

Output:

`Yes`

Time Complexity: O(N*M), as we are using nested loops for traversing the matrix.
Auxiliary Space: O(1), as we are not using any extra space.

My Personal Notes arrow_drop_up