There is a 2D array of size 3*n.
Suppose there are 3 numbers 1, 2 and 3.
What can be the number of ways in which we can put numbers in 2D array using these numbers only according to below rule
1)All the n cells of a single row do not have the same number
2)All the 3 cells of a single column do not have the same number.
I am trying to calculate answer for this but am not able to find how to calculate or what formula to use.
We can use the principle of inclusion-exclusion to solve this problem. We first determine the amount of ways to fill in the grid if no column holds the same three values. Since there are $3^3=27$ possible ways to fill in each column, $3$ of which are invalid, we have $24^n$ possible combinations.
Next, we determine all combinations where either one of the rows equals only ones, twos or threes. Note that if one value is determined, only eight more possible combinations for each column remain. Subtracting for three rows and three possible values to fill the row with, we arrive at $9 \cdot 8^n$ combinations.
However, we counted options with two rows with the same values twice, and options with three rows with the same values thrice. Counting the number of combinations where there are exactly two rows with the same values, but two different values: ${3 \choose 2}{3 \choose 2}{2 \choose 1}(3^n-3)$. Counting the number of combinations where there are exactly two rows with the same values, but both rows contain the same number: ${3 \choose 1}{3 \choose 2}(2^n - 2)$. Counting the number of combinations where the three rows contain the same values, with three possible numbers for the first row and eight for the remaining two rows: $3 \cdot 8$.
The total number of valid combinations thus equals:
$$24^n - 9 \cdot 8^n + 18 \cdot (3^n - 3) + 9 \cdot (2^n - 2) + 2 \cdot 24$$
$$ = 24^n - 9 \cdot 8^n + 18 \cdot 3^n + 9 \cdot 2^n - 24$$