"HOPEFULLY CORRECT THIS TIME!" METHOD
Step 1: Check if there's 2 or more colours left. If there's none or one colour left you're finished (can't find more pairs).
Step 2: Find one colour that has the lowest non-zero count
Step 3: Excluding the colour with the lowest count (in case all colours have the same count); find the highest count and determine how many colours share the highest count
Step 4: Excluding the colour with the lowest count and all colours with the highest count; try to find the second highest count.
Step 5a: If there is a second highest count, calculate amount_to_pair = min(highest_count - second_highest_count, lowest_count)
.
Step 5b: If there isn't a second highest count, calculate amount_to_pair = lowest_count
.
Step 6: Create amount_to_pair
pairs by pairing socks from the colour with the lowest count with socks with colours that have the highest count as evenly as possible (e.g. if there's 9 red socks, 20 blue socks and 20 green socks; then create 5 "red and blue" pairs and 4 "red and green" pairs).
Step 7: Goto step 1.
Example (the pathological case mentioned in comments):
Initial condition
Color 1 -> 1 socks
Color 2 -> 20 socks
Color 3 -> 80 socks
Color 4 -> 81 socks
First iteration:
Color 1 -> 1 socks (lowest non-zero count)
Color 2 -> 20 socks
Color 3 -> 80 socks (2nd highest count)
Color 4 -> 81 socks (highest count)
Amount to remove = min(81-80, 1) = 1
Color 1 -> 1-1=0 socks (lowest non-zero count)
Color 2 -> 20 socks
Color 3 -> 80 socks
Color 4 -> 81-1=80 socks (highest count)
Results so far:
(1 pair of colour 1 and colour 4)
Second iteration:
Color 1 -> 0 socks
Color 2 -> 20 socks (lowest non-zero count)
Color 3 -> 80 socks (highest count)
Color 4 -> 80 socks (highest count)
Amount to remove = 20
Color 1 -> 0 socks
Color 2 -> 20-20=0 socks (lowest non-zero count)
Color 3 -> 80-(20/2)=70 socks (highest count)
Color 4 -> 80-(20-20/2)=70 socks (highest count)
Results so far:
(1 of colour 1 and colour 4)
(10 of colour 2 and colour 3)
(10 of colour 2 and colour 4)
Third iteration:
Color 1 -> 0 socks
Color 2 -> 0 socks
Color 3 -> 70 socks (lowest non-zero count)
Color 4 -> 70 socks (highest count)
Amount to remove = 70
Color 1 -> 0 socks
Color 2 -> 0 socks
Color 3 -> 70-70=0 socks (lowest non-zero count)
Color 4 -> 70-70=0 socks (highest count)
Results so far:
(1 of colour 1 and colour 4)
(10 of colour 2 and colour 3)
(10 of colour 2 and colour 4)
(70 of colour 3 and colour 4)
ORIGINAL METHOD
WARNING: The method below gives wrong results in various pathological cases (and has been updated/replaced by the algorithm above). I've left it here to give context to some of the comments
Starting condition:
Color 1 -> 4 socks
Color 2 -> 3 socks
Color 3 -> 7 socks
Color 4 -> 11 socks
Color 5 -> 4 socks
Find the highest count and lowest count; and cancel out whichever has lowest count so it ceases to exist:
Color 1 -> 4 socks
Color 3 -> 7 socks
Color 4 -> 11-3=8 socks
Color 5 -> 4 socks
Results so far:
(3 of colour 2 and colour 4)
Do it again:
Color 3 -> 7 socks
Color 4 -> 8-4=4 socks
Color 5 -> 4 socks
Results so far:
(3 of colour 2 and colour 4)
(4 of colour 1 and colour 4)
Do it again:
Color 3 -> 7-4=3 socks
Color 5 -> 4 socks
Results so far:
(3 of colour 2 and colour 4)
(4 of colour 1 and colour 4)
(4 of colour 4 and colour 3)
Do it again:
Color 5 -> 4-3=1 sock
Results so far:
(3 of colour 2 and colour 4)
(4 of colour 1 and colour 4)
(4 of colour 4 and colour 3)
(4 of colour 3 and colour 5)
Stop because there's only one colour left.
1, 20, 80, 81
- after 1st iteration you'd get1, 20, 0, 1
and be unable to do much with the 20, and get the answer 82 with 18 socks left over (where the correct answer should've been 91 with no socks left over). – Brendan0, 20, 80, 80
, after 2nd iteration it might be0, 10, 70, 80
, and after 3rd iteration it might be0, 0, 70, 70
. – Brendan