C++ || MATH || EASY TO UNDERSTAND
Intuition\nstore index{i,j} of every element of matrix \n\n# Approach\nfor an element at index x of arr get{i,j} increment r[i] and c[j] by 1\nif r[i] came n ti
# Intuition\nstore index{i,j} of every element of matrix \n\n# Approach\nfor an element at index x of arr get{i,j} increment r[i] and c[j] by 1\nif r[i] came n time than return index x\nif c[j] came m time than return index x\n# Complexity\n- Time complexity:\nO(m*n)\n\n- Space complexity:\nO(m*n)\n\n# Code\n```\nclass Solution {\npublic:\n    int firstCompleteIndex(vector<int>& arr, vector<vector<int>>& mat) {\n        int i,j,m=mat.size(),n=mat[0].size();\n        vector<vector<int>> v(m*n);\n        vector<int> r(m,0),c(n,0);\n        for(i = 0; i < m; i++){\n            for(j = 0; j < n; j++){\n                v[mat[i][j]-1] = {i,j};\n            }\n        }\n        for(i = 0; i < m*n; i++){\n            r[v[arr[i]-1][0]]++;\n            c[v[arr[i]-1][1]]++;\n            // cout<<arr[i]<<" "<<v[arr[i]-1][0]<<" "<<v[arr[i]-1][1]<<" "<<r[v[arr[i]-1][0]]<<" "<<c[v[arr[i]-1][1]]<<endl;\n            if(r[v[arr[i]-1][0]] == n)return i;\n            if(c[v[arr[i]-1][1]] == m)return i;\n        }\n        return -1;\n    }\n};\n```
['Math', 'C++']