I couldn't find a neat general solution for n candies and n/2 jars. It's way beyond my ability. I vaguely remember once looking for but not finding a solution to the menage problem excluding the typical restriction that the seating must alternate man/woman. That seems to be a very similar problem.
I'll show my work. Not because it's any good. I'm just hoping that it might give some ideas to many of you here who are smarter and better educated than me. I doubt if this leads anywhere - so I apologize in advance if this is way off and a complete waste of everyone's time.
First I create a sequence for the color of candies drawn
, and assume they are put in the jars in order: (
in Jar 1,
in Jar 2, ...)
I list the six possible arrangements of four candies in a matrix:
I then have a matrix which describes the match status of each element in the matrix (i.e., whether or not the element matches its partner in the jar).
For odd values of
For even values of
So my "match-status" matrix for n=4 corresponding to the above outcomes is:
There needs to be a second status matrix which shows the potential new partner for each element if that particular element is shifted to the right by one space (assuming one candy is placed to the left and one is placed to the right). Candies can be displaced by 0, 1 or 2 spaces. If they are displaced by 0 or 2 spaces they are paired with the same candy as before. If they are displaced by 1 space, they have a new partner. For odd numbered candies, this new partner will be to the left; for even numbered candies it will be to the right.
For the above matrix, this "alternate status" matrix is as follows:
Now I want to get from a matrix for
to a matrix for
, but there are
possible transformations. Leaving spaces for the possible locations of the next two candies we have
represent the position of
occupies in the expanded string, so
and the empty spaces are possible locations for
represent the transformation matrix when
. There is a 4th case which is a subset of the 3rd case above, and that is when
. In this case,
I can't take it from here. I don't know whether it goes anywhere from here, and I apologize again for my severe shortcomings in linear algebra. I hope it gives someone some ideas on another tack.