Okay, given the constraint that a and b are integers between 0 and 9 (inclusive), we can definitely devise a simple function that maps each unordered pair {a, b} to a unique integer.
However, as we calculated previously, there are 55 unique unordered pairs possible when a and b range from 0 to 9.
- Pairs where
a == b: (0,0), (1,1), ..., (9,9) -> 10 pairs
- Pairs where
a < b: (0,1), (0,2), ..., (0,9) -> 9 pairs
(1,2), (1,3), ..., (1,9) -> 8 pairs
...
(8,9) -> 1 pair
Total = 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 = 45 pairs
- Total unique unordered pairs = 10 + 45 = 55 pairs.
Since there are 55 distinct pairs, it's impossible to map them uniquely to a range containing only 21 integers (like 0 to 20). You need at least 55 distinct output values to guarantee uniqueness for every pair.
We can, however, create a function that maps these 55 pairs uniquely to the integers 0 through 54. This is often called a "pairing function". A common and efficient one for this scenario (where we order the pair first) is based on triangular numbers.
Here's the function mapping to the range [0, 54]:
public class PairEncoder {
/**
* Generates a unique integer ID for an unordered pair {a, b},
* where 0 <= a <= 9 and 0 <= b <= 9.
*
* The function guarantees that getUniqueId(a, b) == getUniqueId(b, a).
* It maps the 55 possible unique unordered pairs to the integers 0 through 54.
*
* It's impossible to map 55 unique pairs into a smaller range like 0-20
* without collisions.
*
* @param a The first integer (0-9).
* @param b The second integer (0-9).
* @return A unique integer ID between 0 and 54 (inclusive).
* @throws IllegalArgumentException if a or b are outside the range [0, 9].
*/
public static int getUniquePairId_0_to_54(int a, int b) {
// Optional: Input validation
if (a < 0 || a > 9 || b < 0 || b > 9) {
throw new IllegalArgumentException("Inputs a and b must be between 0 and 9.");
}
// 1. Establish Canonical Order (smaller number first)
int minVal = Math.min(a, b); // Let this be x
int maxVal = Math.max(a, b); // Let this be y
// 2. Apply Pairing Function (based on triangular numbers)
// Formula: ID = (y * (y + 1) / 2) + x
// This counts pairs systematically:
// (0,0) -> 0
// (0,1) -> 1
// (1,1) -> 2
// (0,2) -> 3
// (1,2) -> 4
// (2,2) -> 5
// ...
// (9,9) -> 54
int id = (maxVal * (maxVal + 1) / 2) + minVal;
return id;
}
// --- Example Usage ---
public static void main(String[] args) {
System.out.printf("ID for (3, 5): %d\n", getUniquePairId_0_to_54(3, 5)); // 18
System.out.printf("ID for (5, 3): %d\n", getUniquePairId_0_to_54(5, 3)); // 18 (Same)
System.out.printf("ID for (0, 0): %d\n", getUniquePairId_0_to_54(0, 0)); // 0
System.out.printf("ID for (0, 1): %d\n", getUniquePairId_0_to_54(0, 1)); // 1
System.out.printf("ID for (1, 0): %d\n", getUniquePairId_0_to_54(1, 0)); // 1 (Same)
System.out.printf("ID for (1, 1): %d\n", getUniquePairId_0_to_54(1, 1)); // 2
System.out.printf("ID for (9, 9): %d\n", getUniquePairId_0_to_54(9, 9)); // 54 (Max ID)
System.out.printf("ID for (8, 9): %d\n", getUniquePairId_0_to_54(8, 9)); // 53
System.out.printf("ID for (9, 8): %d\n", getUniquePairId_0_to_54(9, 8)); // 53 (Same)
// Example of trying an invalid input (will throw exception if uncommented)
// try {
// getUniquePairId_0_to_54(10, 2);
// } catch (IllegalArgumentException e) {
// System.out.println("Caught expected exception: " + e.getMessage());
// }
}
}