 <p>Sounds like it's just some random idea. You can simplify your code by using a one-dimensional array, where each entry in the array is </p> <ul> <li>0 for numbers not in a group</li> <li>1 for the first number of a group</li> <li>2 for the second number of a group</li> </ul> <p>For example, if array[4] is 1 and array[5] is 2, then 4 and 5 are a group.</p> <p>When the user enters a new number, it's easy to update the array. Here's an example in pseudo-code of how the array would be updated if the user enters the number 7</p> <pre><code>if (array[7] == 0 and array[8] == 0) array[7] = 1, array[8] = 2 else if (array[7] == 0 and array[8] == 1) input is invalid else if (array[7] == 1) array[7] = 0, array[8] = 0 else if (array[7] == 2) array[6] = 0, array[7] = 0 </code></pre>