Beats 80.60% | Array | HashMap | Solution for LeetCode#2661
IntuitionThe problem requires us to find the first index in the given array arr where a complete row or column in the matrix mat is filled. We can keep track of
![image.png](https://assets.leetcode.com/users/images/dd70bbb6-b136-434a-9f7b-bd0c94b0e0b6_1737334491.1576827.png)


# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to find the first index in the given array arr where a complete row or column in the matrix mat is filled. We can keep track of the number of filled cells in each row and column, and return the index as soon as any row or column is completely filled.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Create two arrays rowFreq and colFreq to keep track of the number of filled cells in each row and column respectively.
2. Create a HashMap to store the position (row and column) of each element in the matrix.
3. Iterate through the arr array:
	- For each element, get its position in the matrix from the HashMap.
	- Increment the corresponding row and column frequencies.
	- If any row frequency equals the number of columns or any column frequency equals the number of rows, return the current index.
4. If no complete row or column is found, return -1 (though this case should not occur given the problem constraints).

# Complexity
- Time complexity: O(m*n + k), where k is the size of arr
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(m⋅n + m + n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        int m=mat.length;
        int n=mat[0].length;

        //Declare array frequency for Row and Column
        int[] rowFeq=new int[m]; 
        int[] colFeq=new int[n]; 

        //Set HashMap for the Matrix value corresponding row Index and column Index 
        Map<Integer, int[]> map=new HashMap<>();
        for(int i=0; i<m; i++){
            for(int j=0; j<n; j++){
                map.put(mat[i][j], new int[]{i,j});
            }
        }
        
        //Traversing the "arr", painting the "mat" cell and increasing the "rowFeq" and "colFeq"
        for(int i=0; i<arr.length; i++){
            int[] idx=map.get(arr[i]);
            int r=idx[0];
            int c=idx[1];

            rowFeq[r]++;
            colFeq[c]++;

            //rowFeq[r] will be equal of no. of columns
            //colFeq[c] will be equal of no. of rows
            if(rowFeq[r]==n || colFeq[c]==m){ 
                return i;
            }
        }

        return -1;
    }

}
```
```python []
class Solution(object):
    def firstCompleteIndex(self, arr, mat):
        """
        :type arr: List[int]
        :type mat: List[List[int]]
        :rtype: int
        """
        m = len(mat)
        n = len(mat[0])

        # Declare array frequency for Row and Column
        rowFeq = [0] * m
        colFeq = [0] * n

        # Set HashMap for the Matrix value corresponding row Index and column Index 
        map = {}
        for i in range(m):
            for j in range(n):
                map[mat[i][j]] = [i, j]
        
        # Traversing the "arr", painting the "mat" cell and increasing the "rowFeq" and "colFeq"
        for i in range(len(arr)):
            idx = map[arr[i]]
            r = idx[0]
            c = idx[1]

            rowFeq[r] += 1
            colFeq[c] += 1

            # rowFeq[r] will be equal of no. of columns
            # colFeq[c] will be equal of no. of rows
            if rowFeq[r] == n or colFeq[c] == m:
                return i

        return -1

        
```
['Hash Table', 'Python', 'Java', 'Python3']