Simple Approach || Step by Step Explanation || Beats 100%
Intuition
Since the 2-d array mat contains elements from 1 to m*n, we can try thinking of a approach where we can take advantage of this constraint.
Also try th
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
1. Since the 2-d array mat contains elements from 1 to m*n, we can try thinking of a approach where we can take advantage of this constraint.
2. Also try thinking about thinking row wise and column wise separately, this can help in building the approach on how to simulate the updates and check whether an entire row or column has been filled.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Since elements are constrained between 1 to n*m, store the row and column values for each element of mat array in a 2-d array rowcol, which can be further used while we traverse in array arr.
2. Maintain two arrays for rows and columns, namely row(n, 0) and col(m, 0) check initially initialised to zero as no element has been processed yet.
3. Now start traversing in the array arr, let x = arr[i], find the row value and column value from rowcol[x], and increase count of row[row_value] and col[col_value] by 1.
4. If row[row_value] becomes m, this means all elements of this row has been filled.
Similarly, if col[col_value] becomes n, this means all elements of this column has been filled.
5. Return the index whenever you find any of the conditions in step 4 to be true.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n*m) + O(ans), since we will traverse in array arr till ans index only.
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(n*m) for rowcol 2-d array.
O(n) + O(m) for row and col arrays for maintaining counts processed.
# Code
```cpp []
class Solution {
public:
    int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {
        int n = mat.size(), m = mat[0].size();
        vector<pair<int, int>> rowcol(n*m + 1);
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                rowcol[mat[i][j]] = {i, j};
            }
        }
        vector<int> row(n, 0), col(m, 0);
        for(int i=0; i<arr.size(); i++){
            int r = rowcol[arr[i]].first;
            int c = rowcol[arr[i]].second;
            row[r]++;
            col[c]++;
            if(row[r] == m || col[c] == n) return i; 
        }
        return 0;
    }
};
```
['Array', 'Hash Table', 'Matrix', 'C++']