How do I find the count of sub-arrays whose sum of elements is divisible by $3$? The elements are to be chosen from $0,1,2$. These elements can appear any number of time in array. The number of appearance of the elements is also given.
For example: $$[0,1,2]$$ Number of 0's = 1 Number of 1's = 1 Number of 2's = 1
Answer is $4$: as valid sub-arrays are $$[], [0], [1,2], [0,1,2] $$
Note: Instead of generating all the possible sub-arrays, looking for a way to compute the subset count by using the appearance count of elements, e.g., occurrence of 0's, 1's, and 2's
Looked into following but couldn't use it for the problem: How do I count the subsets of a set whose number of elements is divisible by 3? 4?
I take the liberty of tackling this question from a different (and to my opinion, more useful) viewpoint.
let $\epsilon_i$ be independent identically distributed random variables that distribute $\epsilon_i\sim\text{Uniform}(\{0,1,2\})$. Now define $$S_n=\sum_{i=1}^n\epsilon_i \mod{3}$$ By induction, it is quite easy to see that $S_n\sim\text{Uniform}(\{0,1,2\})$ (can you prove it?). Hence $\mathbb{P}(S_n=0)=\mathbb{P}(3\text{ diviedes }\sum_{i=1}^n\epsilon_i)=1/3$. Now to go back from probability to counting, we multiply by the cardinality of the whole probability space; $3^N$ in our case. Thus the answer is $3^N\cdot 1/3=3^{N-1}$.