count of subsets with sum divisible by $3$ 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?
 A: 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}$.
A: PARTIAL SOLUTION
Happy to hear better solutions.
My answer is: approximately 1/3 the total count of coins in the boxes. The approximation is better the more coins exist.
(1) If all the boxes have exactly one coin, then there surely exists an exact answer.
(2) If all the boxes have at most one coin, then there likely exists an exact answer: count only the boxes with exactly one coin, then proceed as in Case 1 above. Here is my logic:

*

*Let $E$ be the number of empty boxes.

*Copy each of the original subsets from Case 1.

*To each copied subset, append a set containing any number $e = E$ of empty boxes and no non-empty boxes.

*Each copied subset has the same total count of coins as its original subset.

*Therefore, the probability that a copied subset will have a coin count divisible by 3 is equal to the analogous probability for its original subset.

*Therefore, the union of the family of original subsets with the family of copied subsets has the same probability as the family of original subsets.

*The above logic holds true for any subset with $e$ empty boxes (there is exactly one such subset when $e = E$).

*Decrement $e$ by 1: $e = e - 1$.

*The above logic still holds (even when $e > 1$ and there is more than one empty subset with $e$ empty boxes).

*Loop through the argument above until $e = 0$. The above logic still holds.

*At any point above, the probability can be converted into a count by multiplying the probability by the number of subsets.

(3) If there is even one box containing two coins, then I do not have an exact answer. But inputing a suitable set of boxes (i.e., total number of boxes <= 200) into any dynamic programming solution to the subset sum problem (see online) will show that the empirical probability approaches 1/3 as well. And as in Case 2, the probability can be converted into a count very easily.
