Example 18 (1991 National High School League Question) Let $S=\{1,2, \cdots, n\}, A$ be an arithmetic sequence with at least two terms and a positive common difference, all of whose terms are in $S$, and such that when the other elements of $S$ are placed before or after $A$, they cannot form an arithmetic sequence with the same common difference as $A$. Find the number of such $A$ (sequences with only two terms are also considered arithmetic sequences).





Translating the problem statement while preserving the original formatting and line breaks, the result is as follows:

Example 18 (1991 National High School League Question) Let $S=\{1,2, \cdots, n\}, A$ be an arithmetic sequence with at least two terms and a positive common difference, all of whose terms are in $S$, and such that when the other elements of $S$ are placed before or after $A$, they cannot form an arithmetic sequence with the same common difference as $A$. Find the number of such $A$ (sequences with only two terms are also considered arithmetic sequences).
When $n=2 k$ is even, each sequence $A$ that meets the requirements must have two consecutive terms, with the former in $[1,2, \cdots, k]$ and the latter in $\{k+1, k+2, \cdots, n\}$. Conversely, by arbitrarily selecting one number from $\{1,2, \cdots, k\}$ and one from $\{k+1, k+2, \cdots, n\}$, and using their difference as the common difference, we can construct a sequence $A$ that meets the requirements. This correspondence is a bijection (or one-to-one mapping). Therefore, the number of $A$ is $k^{2}=\frac{1}{4} n^{2}$.

When $n=2 k+1$ is odd, the situation is entirely similar. The only difference is that $\{k+1, k+2, \cdots, n\}$ has $k+1$ elements, so the number of $A$ is $k(k+1)=\frac{1}{4}\left(n^{2}-1\right)$.