Easy to understand C++ approach
IntuitionHere our intuition is that we will have to keep track of the number of elements coloured for each row and each column so that we can figure out when a
# Intuition
Here our intuition is that we will have to keep track of the number of elements coloured for each row and each column so that we can figure out when a full row or column has been coloured. If we do it in a brute force way, then it will take a lot of time which we do not want. So we need to think of another approach.

# Approach

The approach begins by creating a map to store the positions of all elements in the grid (mat) as (row, column) pairs. This allows us to quickly locate an element's position without repeatedly searching through the grid. Next, we initialize two arrays, rowsele and colsele, to count how many elements have been marked in each row and column, respectively. The sizes of these arrays match the number of rows and columns in the grid. We then iterate through the array arr and, for each element, use the map to find its position in the grid. We increment the corresponding counts in rowsele and colsele and keep track of the maximum counts for any row or column. If at any point, a row or column is fully marked (i.e., the count matches the total number of columns or rows), we return the current index of arr as the result. If no row or column is fully marked by the end of the array, we return -1.
Note that here if I could have returned anything instead of -1 because that line would never have been executed. The line return -1 is included as a fallback because the function requires a return value in all cases. However, in the context of this specific problem, it will never actually be executed.

# Complexity
- Time complexity:
O(m * n)

- Space complexity:
O(m * n)

# Code
```cpp []
class Solution {
public:
    int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {
        
        int rownum = mat.size(), colnum = mat[0].size();
        int rowsize = mat[0].size(), colsize = mat.size();
        vector<int> rowsele(rownum, 0), colsele(colnum, 0);
        unordered_map<int, pair<int, int>> mp;
        for( int i = 0; i < mat.size(); i++ ){
            for( int j = 0; j < mat[0].size(); j++ ){
                mp[mat[i][j]] = {i, j};
            }
        }
        int maxrow = -1, maxcol = -1;
        for( int i = 0; i < arr.size(); i++ ){
            int row = mp[arr[i]].first, col = mp[arr[i]].second;
            rowsele[row]++;
            colsele[col]++;
            maxrow = max(maxrow, rowsele[row]);
            maxcol = max(maxcol, colsele[col]);
            if(maxrow == rowsize || maxcol == colsize){
                return i;
            }
        }
        return -1;
    }
};
```
['C++']