This is not very efficiency. It's start at 0 to n-1 to find out the correct tile, it's not necessary, can you store the count and reuse it?