rowCount and colCount, to keep track of the number of painted cells in each row and column.arr.
a. For each element, retrieve its position from the HashMap.
b. Increment the corresponding rowCount and colCount.
c. If any rowCount or colCount reaches the total number of columns or rows, record the index.