Easy Solution Ever
IntuitionThe problem involves finding the first complete index in an array arr when iterating through the elements sequentially and filling rows or columns of a
# Intuition
The problem involves finding the first complete index in an array arr when iterating through the elements sequentially and filling rows or columns of a matrix mat. A row or column is considered complete if all its elements have been encountered in arr at least once.

The approach is to map the positions of matrix elements for quick lookup and track the progress of rows and columns as elements from arr are encountered.
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
(1) Input Representation:

mat is a 2D matrix where each element is distinct.
arr is a sequence of integers representing the order in which elements are "filled."

(2) Mapping Matrix Elements:

To quickly locate the row and column of any element in mat, we create two mapping arrays:
rowIndex: Maps each element in mat to its row index.
colIndex: Maps each element in mat to its column index.
Iterate over the matrix mat, and for each element, store its row and column indices in rowIndex and colIndex.

(3) Tracking Progress:

Use two arrays, rowArray and colArray, to track the count of elements encountered for each row and column, respectively.
rowArray[i] indicates how many elements of row i have been filled.
colArray[j] indicates how many elements of column j have been filled.

(4) Processing the Array arr:

Traverse through the array arr. For each element:
Retrieve its row and column indices using rowIndex and colIndex.
Increment the counters for its corresponding row (rowArray) and column (colArray).

(6) After updating the counters:

Check if the current row or column has become "complete" (i.e., its count equals the total number of columns or rows in mat).
If a row or column is complete, return the current index from arr (indicating the first time a row or column becomes fully filled).

(7) Return Value:

If no row or column becomes complete during the traversal of arr, return 0 (though the problem guarantees that there will always be a valid solution).
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
- Mapping Matrix Elements: 
𝑂(𝑚 × 𝑛)
O(m × n), where 𝑚 is the number of rows and n is the number of columns in mat.
Processing Array arr: 𝑂(𝑘)
O(k), where k is the length of arr.
Overall: 𝑂(𝑚 × 𝑛 + 𝑘)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
- O(m × n + m + n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        
        int[] rowArray = new int[mat.length];
        int[] colArray = new int[mat[0].length];

        int[] rowIndex = new int[mat.length * mat[0].length];
        int[] colIndex = new int[rowIndex.length];

         for(int i=0; i<mat.length; i++){
            for(int j=0; j<mat[0].length; j++){
                int target = mat[i][j];
                rowIndex[target - 1] = i;
                colIndex[target - 1] = j;
            }
        }

        for(int i=0; i<arr.length; i++){
            int target = arr[i];
            
            // for checking row count
            rowArray[rowIndex[target-1]]++;
            if(rowArray[rowIndex[target-1]] == mat[0].length){
                return i;
            }

            colArray[colIndex[target-1]]++;
            if(colArray[colIndex[target-1]] == mat.length){
                return i;
            }
            
        }
        return 0;
        }
}
```
['Java']