How to count the number of x in a rows in a larger set.

39 Views Asked by At

For example, I have 4 in a row like so:

xxxx

I can see that it has 2 xxx in it and 3 xx. Is there a way to quickly get how much of each 'x' in a row there are for any length of x's?

So for $N$ x in a row, how many 'x' in a row of each length are there from $N-1$ to $2$.

2

There are 2 best solutions below

0
On BEST ANSWER

For $N$ x in a row, we have $N$ x, we have $N-1$ of 2 x's in a row, N-2 of 3 x's in a row.

In general we have $N-k+1$ of $k$ x's in a row.

To see why is that so, construct a window of length $k$ and move it.

0
On

$N-1\;\; x's$ of length $2 [1-2\;\;thru\;\; (N-1)-N]$

$N-2\;\; x's$ of length $3 [1-2\;\;thru\;\; (N-2)-N]$

.....

.....

$ 2\;\; x's$ of length $N-1 [1-(N-1)\;\;thru\;\; (N-1)-N]$

sum $ = [1+2+3+........(N-1)] - 1$

You must be knowing the formula for the sum of terms within the brackets $[\;\;\;]$

Added: a combinatorial approach

Consider a string of $N$ balls in which you choose all combinations of $2$ balls, and define its length as the span of balls including the $2$ chosen, viz. if white balls denote the $2$ chosen in the diagram alongside,$\;\;\Large\circ\bullet\bullet\bullet\circ\;\;$ the length of this string is $5$

Since the string of length $N$ is to be excluded, answer = $\binom{N}{2} - 1$