2

I'm reading Sedgewick's "Algorithms" and completely stuck at one exercise. It is formulated like that:

Develop an appropriate mathematical model describing the number of triples of N random int values that sum to 0, where the values are uniformly distributed between –M and M, where M is not small

I wrote a program to calculate such triplets. It iterates through all possible distinct triplets in array A of N numbers. A may have repeating numbers, but these numbers are form uniform random generator.

Example:

A = [7, -3, -4, 0] gives 4 distinct triplets: {7, -3, -4}, {7, -3, 0}, {7, -4, 0}, {-3, -4, 0}. We have only one triplet (first) that sums to 0.

I already calculated the number of 3-samples, it's sampling without replacement and without order: N! / 3! (N - 3)!, but I have no idea how to formulate quantity of triplets that sum to zero.

I want a model and mathematical basis, to calculate average quantity of such triplets among all 3-samples from N.

  • 3-samples and triples are not the same thing, in most definitions - a triple is an ordered sequence of three numbers. Also, nothing said that the values in the triple have to be distinct. – Thomas Andrews Feb 10 '14 at 20:43
  • "Triplets" in my case are just samples of three numbers. And, yes, values may not be distinct. – oroboros Feb 10 '14 at 20:50
  • So is there a distinction between the triplet $\langle-1,-1,2\rangle$ and $\langle-1,2,-1\rangle$? – Thomas Andrews Feb 10 '14 at 20:54
  • It does not depend on values (which are just uniformly distributed). Set A may have repeating numbers. Your two triplets should consist of different elements from A, e.g. {a[0], a[1], a[2]} and {a[0], a[1], a[3]} are distinct. But {a[0], a[1], a[2]} and {a[1], a[2], a[0]} are not considered in this task. – oroboros Feb 10 '14 at 21:00
  • "triples of $N$ random int values$\ldots$where the values are uniformly distributed between $-M$ and $M\ldots$. To me, if say $N$ is $4$ and $M$ is large, then one of these things is something like $({1,1,1,-3}, {1,2,3,-6}, {2,2,1,-5})$. Is that right? It looks like there is confusion between what role $N$ has and what the role of $3$ is. – 2'5 9'2 Feb 10 '14 at 21:28
  • @alex.jordan N is the size of set, we should select only 3 distinct elements at a time. If N = 4, we have set like [34,76,-10,3]. From set we can select triples: ({34,76,-10}, {34,76,3}, {34,-10,3}, {76,-10,3}). – oroboros Feb 10 '14 at 22:19

1 Answers1

3

We are counting the number of pairs $(A, t)$ where $A = (A_1, \dots, A_N)$ is a sequence of $N$ numbers in the range $[-M, M]$ and $t$ is a subset of $\{1, \dots, N\}$ of size $3$ such that $\sum_{i\in t} A_i = 0$.

We first pick a triple (this can be done in ${N \choose 3}$ ways), then pick three values from $[-M, M]$ that sum to $0$ (this can be done in $3M^2 +3M+1$). Finally we pick the values of the remaining $N-3$ positions (can be done in $(2M+1)^{N-3}$ ways).

The total is thus ${N \choose 3}(3M^2+3M+1)(2M+1)^{N-3}$, and the average number of $t$'s per $A$ is therefore

$\frac{{N \choose 3}(3M^2+3M+1)(2M+1)^{N-3}}{(2M+1)^N} = \frac{{N\choose 3}(3M^2+3M+1)}{(2M+1)^3} \sim \frac{1}{16} \frac{N^3}{M}$.

EDIT: I fixed the count of triples from $[-M,M]$ with sum $0$, and calculated the average number of $t$'s, which is what the OP asked about (rather than the probability that a random triple of a random int vector will have sum zero).

Eric
  • 555
  • Is number of triplets = N * 3/16M ? Or I should multiply "total" and "fraction" instead? – oroboros Mar 04 '14 at 08:50
  • Based on my program statistics quantity of zero triplets = N^3 / 16M. Looks like, there must be error in your answer (or in my understanding of it). – oroboros Mar 04 '14 at 09:31
  • 1
    You are right - I made a mistake in the calculation (see edit for details). – Eric Mar 04 '14 at 18:54
  • "then pick three values from $[−M,M]$ that sum to $0$ (this can be done in $3M^2+3M+1$)", why? – razzak Aug 29 '19 at 20:35