First Completely Painted Row or Column - Easy Explanation - Unique Solution 💯
Intuition

Finding the maximum latest index at which all the elements in the specific row or column will be painted.


Then find the minimum of the maximum inde
# Intuition
1. Finding the maximum latest index at which all the elements in the specific row or column will be painted.

1. Then find the minimum of the maximum index visited so far so that we can find the smallest index at which specific row or column that can be painted.
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach

**Initialization**:

Initialize n to be length of the integer array arr.

    int n=arr.length;

Initialize map integer array with the size of arr length plus 1 to store the index of the specific array arr.

    int[] map=new int[n+1];

Initialize row and col to be the number of rows and columns in the matrix mat.

    int row=mat.length;
    int col=mat[0].length;

Initialize ans to the Integer.MAX_VALUE to keep track of minimum index at which all elements in the specific row or column is painted.

    int ans=Integer.MAX_VALUE;

**Store the index**:

Iterate through all the elements of the integer array arr and store index of each element in the integer array arr in the array map.

    for(int i=0;i<n;i++)
    {
        map[arr[i]]=i;
    }

**Iterate through each rows**:

Iterate through all the elements in the matrix mat row-wise.

Outer loop is used to iterate over the row at each iteration, initialize the max to 0 and inner loop is used to iterate over the column.

    for(int i=0;i<row;i++){
        int max=0;
        for(int j=0;j<col;j++){

For each element mat[i][j], retrieve its index from map. This tells us when this element will be painted in the sequence arr and max stores the latest index at which all elements in this row will be painted.

    max=Math.max(max,map[mat[i][j]]);

After processing the row, ans is updated to store the minimum max seen so far.

    ans=Math.min(ans,max);

**Iterate through each column**:

Iterate through all the elements in the matrix mat column-wise.

Outer loop is used to iterate over the column at each iteration, initialize the max to 0 and inner loop is used to iterate over the row.

    for(int i=0;i<col;i++){
        int max=0;
        for(int j=0;j<row;j++){

For each element mat[j][i], retrieve its index from map. This tells us when this element will be painted in the sequence arr and max stores the latest index at which all elements in this column will be painted.

    max=Math.max(max,map[mat[j][i]]);

After processing the row, ans is updated to store the minimum max seen so far.

    ans=Math.min(ans,max);

**Returning the value**:

ans represents the smallest index at which either a row or a column in the matrix becomes completely painted.

    return ans;

<!-- Describe your approach to solving the problem. -->

# Complexity
- ***Time complexity***: **O(mxn)**.
   m -> number of rows in the matrix mat.
   n -> number of columns in the matrix mat.

**Mapping**: **O(mxn)**.

Here this loop iterates over all the elements in the array arr which contains **mxn elements**.

**Processing rows and columns**: **O(mxn)**.

**Rows**: **O(mxn)** (each row has n elements, and there are m rows).
**Columns**: **O(mxn)** (each column has m elements, and there are n columns).

**Overall Time Complexity**: **O(mxn)**.

The overall Time Complexity is contributed by Mapping and Processing rows and columns as **O(mxn) + O(mxn) which takes O(mxn) time**.
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- ***Space complexity***: **O(mxn)**.
   m -> number of rows in the matrix mat.
   n -> number of columns in the matrix mat.

**map array**: **O(mxn)**.

map array which stores index of all the elements in the integer array arr whose size is mxn.Hence it takes **O(mxn) space**.

**Other variables**: **O(1)**.

Other variables such as n,row,col,ans,max which takes **constant space**.Hence the space complexity is **O(1)**.

**Overall Space Complexity**: **O(mxn)**.

Overall Space Complexity is contributed by the map integer array.Hence it takes **O(mxn) space in total**.
 <!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        int n=arr.length;
        int[] map=new int[n+1];
        int row=mat.length;
        int col=mat[0].length;
        int ans=Integer.MAX_VALUE;
        //Store the index of each element in the array arr
        for(int i=0;i<n;i++)
        {
            map[arr[i]]=i;
        }
        //Iterate through each row columnwise
        for(int i=0;i<row;i++)
        {
            int max=0;
            for(int j=0;j<col;j++)
            {
                //stores latest index at which all the elements in the row will be   painted
               max=Math.max(max,map[mat[i][j]]);
            }
            //ans is used to store minimum max seen so far
            ans=Math.min(ans,max);
        }

        for(int i=0;i<col;i++)
        {
            int max=0;
            for(int j=0;j<row;j++)
            {
                max=Math.max(max,map[mat[j][i]]);
            }
            ans=Math.min(ans,max);
        }
        return ans;

    }
}
```
['Array', 'Matrix', 'Java']