Suppose that I have the following array:
array = [0, 1, 2, ... , n]
How do I find a set such that the pairs in the set are all unique and non-repeated elements?
This means that:
• (x, y) = (y, x) so if (x, y) is in the set, then (y, x) would not be & vice-versa
• If an element is already used, it cannot be used again.
E.g: if (1,2) is in the set, then there cannot be a pair in the set that has 1 or 2.
Context: I'm creating a memory game that places coins on a board of 2n elements. I want each iteration of the game to place the elements in random spaces on the board.
E.g:
Suppose I have: [A, B, C, D, E, F]
Since [A, B, C, D, E, F] is length 6, then the board will consist of 12 elements.
My board will look like the following such that the elements were randomly placed:
A B C D
C A F B
E F E D
I frankly don't know how to solve this without doing an O(n^2) brute force method. I figure there might be another algorithm that would be more efficient.