Check if any row of the matrix can be converted to the elements present in the target row
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++
#include <bits/stdc++.h>
using namespace std;
int checkPossibleOrNot(vector<vector< int > >& mat,
vector< int >& target)
{
vector< int > comp(mat[0].size(), INT_MIN);
for ( auto val : mat) {
if (val[0] <= target[0] && val[1] <= target[1]
&& val[2] <= target[2])
comp = { max(comp[0], val[0]),
max(comp[1], val[1]),
max(comp[2], val[2]) };
}
return (comp == target);
}
int main()
{
vector<vector< int > > 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;
}
|
Java
import java.util.*;
class GFG {
public static boolean checkPossibleOrNot(
int [][] mat, int [] target)
{
int [] comp = new int [(mat[ 0 ].length)];
Arrays.fill(comp, Integer.MIN_VALUE);
for ( int [] val : mat) {
if (val[ 0 ] <= target[ 0 ]
&& val[ 1 ] <= target[ 1 ]
&& val[ 2 ] <= target[ 2 ])
comp = new int [] { Math.max(
comp[ 0 ], val[ 0 ]),
Math.max(
comp[ 1 ], val[ 1 ]),
Math.max(
comp[ 2 ], val[ 2 ]) };
}
return Arrays.equals(comp, target);
}
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
import sys
def checkPossibleOrNot(mat, target) :
comp = [] ;
INT_MIN = - (sys.maxsize - 1 )
for i in range ( len (mat[ 0 ])) :
comp.append(INT_MIN)
for val in mat :
if (val[ 0 ] < = target[ 0 ] and val[ 1 ] < = target[ 1 ] and val[ 2 ] < = target[ 2 ]) :
comp = [ max (comp[ 0 ], val[ 0 ]), max (comp[ 1 ], val[ 1 ]), max (comp[ 2 ], val[ 2 ]) ];
if comp = = target :
return True
else :
return False
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" )
|
C#
using System;
using System.Linq;
public class GFG {
public static bool checkPossibleOrNot(
int [,]mat, int [] target)
{
int [] comp = new int [(mat.GetLength(0))];
for ( int i = 0; i < mat.GetLength(0); i++)
comp[i] = int .MinValue;
for ( int i = 0; i < mat.GetLength(0); i++){
int []val = {mat[i, 0], mat[i, 1], mat[i, 2]};
if (val[0] <= target[0]
&& val[1] <= target[1]
&& val[2] <= target[2])
comp = new int [] { Math.Max(
comp[0], val[0]),
Math.Max(
comp[1], val[1]),
Math.Max(
comp[2], val[2]) };
}
return comp.SequenceEqual(target);
}
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);
}
}
|
Javascript
<script>
function checkPossibleOrNot(mat, target)
{
let comp = new Array(mat[0].length).fill(Number.MIN_SAFE_INTEGER);
for (let val of mat)
{
if (val[0] <= target[0] && val[1] <= target[1] && val[2] <= target[2])
comp = [
Math.max(comp[0], val[0]),
Math.max(comp[1], val[1]),
Math.max(comp[2], val[2]),
];
}
return comp.join( "" ) == target.join( "" );
}
let mat = [
[2, 5, 3],
[2, 8, 4],
[1, 5, 7],
];
let target = [2, 5, 7];
let ans = checkPossibleOrNot(mat, target) ? "Yes" : "NO" ;
document.write(ans);
</script>
|
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.
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...