Solution:
We solve directly the general case, showing that a subset S is identifiable if and only if 0∈/S and 101∈S. At the end we will describe a shortcut that works in the odd case.
Necessary condition
Let us first show that the conditions 0∈/S and 101∈S are necessary. If, for contradiction, 0∈S and at the beginning there are no balls in the glasses, no program can make a ball appear in the glass in position 1, as would instead be required. Symmetrically, if 101∈/S and at the beginning all glasses contain a ball, then the configuration remains the same throughout the execution of the program, so that at the end there will certainly also be a ball in the glass in position 1, which should not happen in this case.
Sufficient condition
We now show that the conditions 0∈/S and 101∈S are sufficient. We denote by p the number of glasses that contain a ball. As already observed, if p=0 or p=101, all possible instructions do not alter the configuration, which in both cases already satisfies the requirement from the start. In what follows we will therefore assume, without loss of generality, that 1≤p≤100.
We say that the glasses are arranged in standard canonical position if the glasses with the ball occupy the positions from 1 to p; we say that they are arranged in shifted canonical position if they occupy the positions from 2 to p+1. We will show that there exists a program upon execution of which the final configuration will be the standard canonical one if p∈S, and will be the shifted canonical one if p∈/S (so in particular there will be a ball in the glass in position 1 if and only if p∈S).
We divide the required program into three subprograms. The first subprogram passes from the initial configuration to the standard canonical configuration. One way to achieve this is the following list of instructions.
- For every i from 1 to 101 we execute the instruction (i;i,1). If there is a ball somewhere, at the end there will also be a ball in the glass in position 1.
- For every i from 2 to 101 we execute the instruction (i;i,2). If there is at least one ball besides the one in the glass in position 1, at the end there will also be a ball in the glass in position 2.
- For every i from 3 to 101 we execute the instruction (i;i,3). If there is at least one ball besides those possibly present in the glasses in position 1 and 2, at the end there will also be a ball in the glass in position 3.
- Continuing in the same way we obtain the required result (more formally, this could be proved by induction).
The second subprogram passes from the standard canonical configuration to the shifted canonical one. To do this we execute the instruction (i;i,i+1) for every i from 100 to 1, proceeding thus in reverse. These instructions do nothing as long as i>p. When i=p, the glass with the ball in position p is moved to position p+1, then the one in position p−1 is moved to position p, and so on until the glass with the ball in position 1 is moved to position 2.
The third subprogram starts from the shifted canonical configuration. Given an integer s, with 1≤s≤100, we define s-check as the pair of instructions (s+1;s+1,1) and (s+2;s+1,1) (in the case s=100 the second instruction does not make sense, so only the first is executed). Let us examine the effect of an s-check in three cases.
- Case 1. If we are in the shifted canonical configuration and s=p, then essentially nothing happens. More precisely, if s>p neither instruction finds the ball and so neither does anything, if s<p both instructions find the ball and so the second one cancels the effect of the first.
- Case 2. If we are in the shifted canonical configuration and s=p, then the first instruction moves the glass with the ball that is in position p+1 (that is, the last glass with a ball in the row) to position 1, while the second instruction does not find the ball and so does nothing. Consequently, in this case the s-check has the effect of passing from the shifted canonical configuration to the standard canonical configuration.
- Case 3. If we are in the standard canonical configuration and s<p, then nothing happens, because the possible swaps take place between glasses that contain a ball.
We are now ready to describe the third subprogram, which executes the s-check for all values s∈S, proceeding from the largest to the smallest. We have two possibilities.
- If p∈/S, then we are always in Case 1. Hence nothing ever happens, and the configuration remains the shifted canonical one, as expected in the case p∈/S.
- If p∈S, then nothing happens as long as values s>p are being tested (Case 1), then when s=p the configuration becomes the standard canonical one (Case 2), and then again nothing happens when the values s<p are tested (Case 3). Hence at the end of the execution the configuration is the standard canonical one, as expected in the case p∈S.
Shortcut for the odd case
In the case where S is the set of odd numbers from 1 to 101, it suffices to execute the first subprogram (the one that brings the glasses into the standard canonical configuration) and then execute the instruction (i;1,101) for every i from 2 to 101.
- If the number of balls is odd, the swap is executed an even number of times, and so at the end the standard canonical configuration remains, which has the ball in the glass in position 1.
- If the number of balls is even, the swap is executed an odd number of times, and so at the end the glass in position 1 will be the one that in the standard canonical configuration was in position 101, which was empty (since p is even).
Remark
In the solution of the general case one can pass directly from the initial configuration to the shifted canonical one, without going through the standard canonical one. It suffices to execute a version of the first subprogram in which all the indices are cyclically shifted 1→2→3→…→100→101→1, as if the row started from position 2 to end at position 1 "after going around".