Sunday, January 24, 2016

Pair socks from pile

Question: How to find pair socks from a pile efficiently?

Resource: http://stackoverflow.com/questions/14415881/pair-socks-from-a-pile-efficiently
Question Owner: amit




ANSWER


Actually, you can do this by selecting one sock and searching all the pile. It is least efficient way I guess.

Try to think this problem like solving a puzzle. Before to solve a puzzl, first, we group all same colors like "yellows go this pile" or "blue ones go there". This method is called hashing(or maybe only I call this like that I do'nt know :))

After we grouped we can search easily them. Try to think this with letters from 'A' to 'Z'. We have 26 pairs as "A-A" or "J-J".

Consider them as colors and say: "First 13 letters are yellow, and last 13 are blue"

We search all socks(letters) and group them according to their colors.

Then, we can look more carefully and notice their patterns to find pairs.

No comments:

Post a Comment