Concise Explanation of Editorial 🤖
Brute Force Approach
Map the values in the matrix to their coordinates using a hash map.
Iterate through the array arr, marking the corresponding cell in the ma
### Brute Force Approach
1. Map the values in the matrix to their coordinates using a hash map.
2. Iterate through the array `arr`, marking the corresponding cell in the matrix as "visited" (by multiplying it by `-1`).
3. After each update, check if the corresponding row or column is fully visited using helper functions `checkRows` and `checkCols`.
4. If a fully visited row or column is found, return the index of the element in `arr` that caused this.

---

### Optimized Approach (Better Time Complexity)
1. Use a hash map to store the mapping of values in the matrix to their coordinates.
2. Maintain two vectors, `rowCount` and `colCount`, to track the count of visited cells for each row and column.
3. For each element in `arr`, update the count for the corresponding row and column.
4. If the count of any row or column matches its respective size, return the index.

---

### Optimized Approach (Better Space Complexity)
1. Use a hash map to store the index of each value in `arr`.
2. Iterate through each row and column of the matrix, finding the maximum index of the values in `arr` that are part of the current row or column.
3. Track the minimum of these maximum indices to identify the first fully visited row or column.
4. Return the smallest such index.

---

## Complexity Analysis

| **Approach**             | **Time Complexity**                  | **Space Complexity**        |
|---------------------------|--------------------------------------|-----------------------------|
| **Brute Force**           | \(O(n.m + k.(n + m))\)  | \(O(n.m)\)           |
| **Optimized (Better TC)** | \(O(n.m + k)\)                | \(O(n.m + n + m)\)   |
| **Optimized (Better SC)** | \(O(k + n.m)\)                | \(O(k)\)                   |

- \(n\): Number of rows in the matrix.
- \(m\): Number of columns in the matrix.
- \(k\): Size of the array `arr`.

--- 

# Code
 
```cpp [Brute]
class Solution {
private:
    bool checkRows(int row,vector<vector<int>>& mat){
        for(int col = 0;col<mat[0].size();col++){
            if(mat[row][col] > 0){
                return false;
            }
        }
        return true;
    }

    bool checkCols(int col,vector<vector<int>>& mat){
        for(int row = 0;row<mat.size();row++){
            if(mat[row][col] > 0){
                return false;
            }
        }
        return true;
    }

public:
    int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {
        int nCols = mat[0].size();
        int nRows = mat.size();
        unordered_map<int,pair<int,int>>freq;
        vector<int>rowCount(nRows,0),colCount(nCols,0);

        for(int i=0;i<nRows;i++){
            for(int j=0;j<nCols;j++){
                int num = mat[i][j];
                freq[num] = {i,j};
            }
        }

        for(int i=0;i<arr.size();i++){
            int val = arr[i];
            auto [row,col] = freq[val];
            mat[row][col] = mat[row][col] * -1;

            if(checkRows(row,mat) || checkCols(col,mat)){
                return i;
            }
        }
        return -1;
    }
};
```
```cpp [Better(TC)]
class Solution {
public:
    int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {
        int nCols = mat[0].size();
        int nRows = mat.size();
        unordered_map<int,pair<int,int>>freq;
        vector<int>rowCount(nRows,0),colCount(nCols,0);

        for(int i=0;i<nRows;i++){
            for(int j=0;j<nCols;j++){
                int num = mat[i][j];
                freq[num] = {i,j};
            }
        }

        for(int i=0;i<arr.size();i++){
            int val = arr[i];
            auto [row,col] = freq[val];
            rowCount[row]++, colCount[col]++;

            if(rowCount[row]==nCols || colCount[col]==nRows){
                return i;
            }
        }
        return -1;
    }
};
```

```cpp [Better(SC)]
class Solution {
public:
    int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {
        int nCols = mat[0].size();
        int nRows = mat.size();
        unordered_map<int,int>freq;

        for(int i=0;i<arr.size();i++){
            freq[arr[i]] = i;
        }

        int minIdx = INT_MAX;

        // Rows
        for(int row=0;row<nRows;row++){
            int maxIdx = INT_MIN;
            for(int col=0;col<nCols;col++){
                int val = mat[row][col];
                int Idx_to_Check = freq[val];
                maxIdx = max(maxIdx,Idx_to_Check);
            }
            minIdx = min(maxIdx,minIdx);
        }

        //Column
        for(int col=0;col<nCols;col++){
            int maxIdx = INT_MIN;
            for(int row=0;row<nRows;row++){
                int val = mat[row][col];
                int Idx_to_Check = freq[val];
                maxIdx = max(maxIdx,Idx_to_Check);
            }
            minIdx = min(maxIdx,minIdx);
        }
        return minIdx;
    }
};
```
['Hash Table', 'Matrix', 'C++']