Simple Java Code✅|| Simple Approach🔥|| Well Explained🚀
IntuitionTo find the smallest index i in arr such that a row or column in mat is completely painted:
Every integer in arr maps uniquely to a cell in mat. This a
# Intuition
To find the smallest index i in arr such that a row or column in mat is completely painted:

1. Every integer in arr maps uniquely to a cell in mat. This allows us to track the painting process.
2. A row is fully painted when all its cells are painted, and similarly for a column.
3. Use counters for rows and columns to keep track of the number of painted cells for each.

---


# Approach
1. Mapping Elements to Coordinates:

    - Create a mapping from the value of each cell in mat to its corresponding [row,column] coordinates using a HashMap.
    
2. Painting Process:

    - Traverse through arr, and for each element:
        - Retrieve its corresponding [row,column] coordinates using the map.
        - Increment the counters for that row and column.
        - Check if the row or column is completely painted:
            - A row is complete if the counter for that row equals the number of columns.
            - A column is complete if the counter for that column equals the number of rows.

3. Return the Result:

    - The first index i in arr where either a row or column is completely painted is the result.

---


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

- Space complexity: O(m⋅n)

---


# Code
```java []
class Solution {
    public int firstCompleteIndex(int[] arr, int[][] mat) {
        Map<Integer, int[]> map = new HashMap<>();
        int rows = mat.length;
        int cols = mat[0].length;
        for(int i=0; i<rows; i++) {
            for(int j=0; j<cols; j++) {
                int[] array = new int[2];
                array[0] = i;
                array[1] = j;
                map.put(mat[i][j], array);
            }
        }

        int[] r = new int[rows];
        int[] c = new int[cols];

        for(int i=0; i<arr.length; i++) {
            int[] a = map.get(arr[i]);
            r[a[0]]++;
            c[a[1]]++;
            if (r[a[0]] == cols || c[a[1]] == rows) {
                return i;
            }
        }
        return 0;
    }
}
```
['Array', 'Hash Table', 'Matrix', 'Java']