in reply to Help regarding an alogrithm

Write down, in the human language you're most comfortable in, how you would explain to a 10 year old how to solve this problem. Translating that into Perl is simple.