The Wayback Machine - https://web.archive.org/web/20211019102706/https://www.geeksforgeeks.org/check-if-any-row-of-the-matrix-can-be-converted-to-the-elements-present-in-the-target-row/
Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 19 Oct, 2021
Geek Week

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:

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 possiblity
        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);
    }
}
Output:
Yes

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

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.




My Personal Notes arrow_drop_up