1

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?

mas
  • 21

2 Answers2

0

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

Alon Yariv
  • 1,363
  • I've updated the question for more clarity, would you please have a look and update the answer, if possible, thanks. – mas Aug 29 '20 at 09:22
  • So you asked a trivial counting question? – Alon Yariv Aug 29 '20 at 09:24
  • 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. Something like this: https://math.stackexchange.com/questions/1721926/number-of-subsets-of-a-set-with-even-sum-using-combinatorics-or-binomial#:~:text=If%20o%3D0%2C%20all%202,%3D2N%E2%88%921%20subsets. However, instead of even sum, here, interested in the sum divisible by 3. – mas Aug 29 '20 at 09:27
  • any insights, pls? – mas Aug 29 '20 at 10:17
  • @AlonYariv

    (1) Finding an exact solution to this variant --- or even the original --- subset sum problem is non-trivial for large sets of boxes.

    (2) It is known that the probability mentioned above is 1/3 only in the limit of an infinitely large set of boxes.

    (3) The discrepancy between your analysis and the empirical probabilities lies in how your uniformly distributed epsilon_i variables correspond only approximately to anything in the real problem. For any N, the count of numbers <= N, grouped by mod 3, are not equal to each other.

    – PDE Sep 01 '20 at 05:05
0

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.

PDE
  • 1,467