On a nice day, Babai, a computer scientist, received back all 6 pairs of my and socks in a laundry basket. He needed to pair them up into pairs of same color for me.
Being a computer scientist, he wondered what would be a good way to sort them out. Can you help him by figuring out the faster way to sort these?
Swap and Go:
- Lay the socks on the table side by side.
- While there are some socks in the last 6 socks, do the following:
- Going from left to right, if there is a sock to the right of a sock, exchange them.
- Now, all 6 pairs of adjacent socks are each of the same color. So, take two at a time and pair them up.
Throw into Buckets:
- Lay the socks on the table side by side. Take two buckets, and .
- For all the socks on the table, check which of them are , one by one:
- if a sock is , put it in the bucket;
- if a sock is , put it in the bucket.
- Now, we know that all the and socks are each in the bucket of the same color. Taking two of them at a time from the same bucket, pair them up.