Determine the generating series of a subset S of $\{1, 2, 3, ..., n\}$ so that consecutive elements of S differ by no more than 2.
My thinking: Try to use compositions. Essentially we have that each part of a composition can be from the set $\{0, 1, 2\}$ if we define $c_i = s_i - s_{i-1}$ for $s \in S$. That is, since the difference is at more 2 between consecutive terms in S, we can have differences of 0, 1, or 2. So those are our choices for each part.
This means that for each part, we get the function $c(x) = 1 + x + x^2$ so with $n$ choices we have $n-1$ possible parts, since they'll represent the value between the $s_1 and s_2$, then $s_2 and s_3$, all the way to $s_{n-1} and s_n$ term, ie there are $n-1$ differences between $n$ subsequent terms.
So, we can get the formula $\sum_{k=0}^{n}(1+x+x^2)^{k-1}=\frac{1}{1+x+x^2}\sum_{k=0}^n(1+x+x^2)^k$ except this is where I get stuck. How can I find a simple formula for a generating function for this series?
I was thinking that the fact that $(1+x)^n = \sum_{k=0}^n\binom{n}{k}x^k$ may come in handy, with $x$ representing $1+x+x^2$ but I'm not really sure how to "get rid of", or account for, the $\binom{n}{k}$ term.
Any help would be appreciated. Thanks!
We consider the set of binary strings of length $n$ with at most one $0$ between consecutive $1$s. Let $a_n$ be the number of strings that start with $0$, and let $b_n$ be the number of strings that start with $1$. Then $a_1=b_1=1$, $b_2=2$, and \begin{align} a_n &= a_{n-1} + b_{n-1} &&\text{for $n \ge 2$}\\ b_n &= b_{n-1} + b_{n-2} + 1 &&\text{for $n \ge 3$} \end{align} (The three cases for $b_n$ correspond to $1$, $01$, and $0\dots0$.) Let $A(z)=\sum_{n=1}^\infty a_n z^n$ and $B(z)=\sum_{n=1}^\infty b_n z^n$. Then the recurrence relations imply \begin{align} A(z)-a_1 z&=z A(z) + z B(z) \\ B(z)-b_1 z-b_2 z^2&=z (B(z) - b_1 z) + z^2 B(z) + \frac{z^3}{1-z} \end{align} Solving for $B(z)$ yields $$B(z)=\frac{z + z^2 + \frac{z^3}{1-z}}{1-z-z^2}=\frac{z}{(1-z)(1-z-z^2)},$$ and solving for $A(z)$ yields $$A(z) = \frac{z(1 + B(z))}{1-z}=\frac{z(1-z+z^3)}{(1-z)^2(1-z-z^2)}.$$ So the desired generating function (including the $1z^0=1$ term for the empty string) is $$1+A(z)+B(z)=\frac{1-z+z^3}{(1-z)^2(1-z-z^2)},$$ which matches both OEIS A000126 and @awkward's answer.