✅🚀 || HashMap || Well explained || JAVA || Clean code
Intuition\n Describe your first thoughts on how to solve this problem. \nHere, all we need to find is the index of the element from arr[i] in the matrix.\nFor e
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHere, all we need to find is the index of the element from arr[i] in the matrix.\nFor every element we need to find the index and to keep the count of number of elements in each row and each column, so the time complexity will be $$O(len*(m*n))$$ where len is length of array.\nNow, let\'s talk about the constraints.\narr.length == m * n\n1 <= m, n <= 10^5\n1 <= m * n <= 10^5\n\nClearly, in the worst case there will be TLE error.\n\nSo, all we need a data structure which can be used to search the index of the arr[i] in a constant time.\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHashMap is the only data structure which can help us to find the index of arr[i] in a constant time.\nWe will be keep tracking the number of elements in each row and each column by using temporary arrays.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(m*n)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(m*n)$$\n\n# Code\n```\nclass Solution {\n    public int firstCompleteIndex(int[] arr, int[][] mat) {\n        \n        int m=mat.length;\n        int n=mat[0].length;\n        \n        int[] rows=new int[m];\n        int[] cols=new int[n];\n        \n        \n        HashMap<Integer,Pair> map=new HashMap<>();\n        \n        for(int i=0;i<m;i++)\n        {\n            for(int j=0;j<n;j++)\n            {\n                map.put(mat[i][j],new Pair(i,j));\n            }\n        }\n        \n        \n        for(int i=0;i<arr.length;i++)\n        {\n            Pair p=map.get(arr[i]);\n            \n            int r=p.i;\n            int c=p.j;\n            \n            rows[r]++;\n            cols[c]++;            \n            \n            if(rows[r]==n || cols[c]==m)\n                return i;\n        }\n        \n        return -1;\n        \n    }\n}\n\nclass Pair\n{\n    int i,j;\n    \n    Pair(int i,int j)\n    {\n        this.i=i;\n        this.j=j;\n    }\n}\n```
['Hash Table', 'Java']