1

Let's say we have elements A and P and I want to group these in groups of n. Let's make an example where n is equal to 3 (groups would be AAP, APA, PPA, etc.).

All possible combinations would be 2 to the power of 3 -> 8.

Now, my problem comes because I cannot have the element A twice in a row.

So all combination are AAA, AAP, APA, APP, PAA, PAP, PPA, and PPP, but answers AAA, AAP, and PAA are excluded because A is repeated consecutively.

What kind of algorithm could I use to find the groups where A is repeated so I can exclude them from all possible combinations? P.S. I have to make this into code, so a code algorithm would be appreciated.

begger
  • 11
  • 1
  • Taking your posting at face value, what you actually want is the Math oriented pseudocode. Let $k$ run from $0$ through $(2^n - 1)$. For each value of $k$, convert $k$ base $(10)$ into $k$ base $(2)$, where the expression is a string of $n$ characters, each of which is either $(0)$ or $(1)$. Within the loop, interrogate each corresponding binary string by traversing it from right to left. Let $d_i$ denote the character in position $(i)$, reading from the right, where $i \in {0,1,2,\cdots,n}$. ...see next comment – user2661923 May 03 '22 at 20:46
  • This means that inside the loop, you will have a nested loop, that has $i$ run from $0$ through $(n-1)$. If $d_i = d_{i+1}$, then abort the outer loop, and move on to the next value of $k$. If the inner loop makes it all the way through, without aborting, then Counter = Counter + 1. – user2661923 May 03 '22 at 20:49
  • Re previous comments, if (for example) you want a list of the unsatisfactory binary strings, then create a container (e.g. in Java, this would be known as an ArrayList) and for each value of $k$ that caused an abortion, add the corresponding binary string to the list of unsatisfactory binary strings. Alternatively, instead of adding the binary string to the list, you can instead add the base 10 representation of the unsatisfactory $k$ to a list. – user2661923 May 03 '22 at 20:55

1 Answers1

1

Let $f(n)$, $g(n)$, $h(n)$ be the number of combinations ending in A, ending in P and the total amount of combinations respectively. We have $f(1)=1$; $g(1)=1$; $h(1)=2$; $f(2)=1$; $g(2)=2$; $h(2)=3$. The number of combinations ending in A of $n+1$ must start with a combination ending in P of $n$ and then end with A. Therefore $f(n+1)=g(n)$. The number of combinations ending in P of $n+1$ either start with a combination ending in P of $n$ or start with a combination ending in A of $n$. Therefore $g(n+1)=f(n)+g(n)$. Also clearly $h(n)=g(n)+f(n)$. So $g(n+1)=f(n)+g(n)=g(n)+g(n-1)$ which has the characteristic equation $x^2-x-1=0$. Solving it you get $g(n)=\frac {5+\sqrt5} {10}(\frac {1+\sqrt5} 2)^n+\frac {5-\sqrt5} {10}(\frac {1-\sqrt5} 2)^n$. Also $f(n+1)=g(n)$ so $f(n)= \frac {5+\sqrt5} {10}(\frac {1+\sqrt5} 2)^{n-1}+\frac {5-\sqrt5} {10}(\frac {1-\sqrt5} 2)^{n-1}$. So the number of combinations is $h(n)=\frac {5+\sqrt5} {10}(\frac {1+\sqrt5} 2)^{n-1}+\frac {5-\sqrt5} {10}(\frac {1-\sqrt5} 2)^{n-1}+\frac {5+\sqrt5} {10}(\frac {1+\sqrt5} 2)^n+\frac {5-\sqrt5} {10}(\frac {1-\sqrt5} 2)^n$

Hi there
  • 76
  • 5