Two pairs: If there are two pairs of dice with the same number, the player scores the sum of these dice. If not, the player scores 0. For example, 1, 1, 2, 3, 3 placed on "two pairs" gives 8.
examples: 1,1,2,3,3 results 8 1,1,2,3,4 results 0 1,1,2,2,2 results 6
How can find this efficiently?
I've been using following code to find a single pair
int max_difference = 0;
int val1 = 0 , val2 = 0;
Arrays.sort(dice);
for (int i = 0; i < dice.length - 1; i++) {
int x = dice[i+1] - dice[i];
if(x <= max_difference) {
max_difference = x;
val1 = dice[i];
val2 = dice[i+1];
}
}
pairScore = val1 + val2;