2D grid of size 3*n

179 Views Asked by At

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.

2

There are 2 best solutions below

0
On BEST ANSWER

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$$

0
On

There are $3^3-3=24$ ways to fill a column so that the numbers are not all the same, so ignoring the row restriction there are $24^n$ ways to fill the grid.

We now subtract the cases with a constant row. There are $3$ ways to choose the row, $3$ ways to choose the common number, and $8$ ways to fill each column to not be constant for $9\cdot 8^n$. We have subtracted grids with two constant rows twice, so have to add them back once. If the two constant rows have the same value there are $3$ ways to choose the rows, $3$ ways to choose the value, and $2^n$ ways to fill the other row. If the values are different, there are $3$ ways to choose the rows, $6$ ways to choose the values, and $3^n$ ways to fill the other row. We add$9\cdot 2^n+18\cdot 3^n$. Now we have counted the $24$ grids with all rows constant once, once, subtracted them three times, and added them three times, so we need to subtract them once. The final number is $$24^n-9\cdot 8^n+9\cdot 2^n+18\cdot 3^n-24$$