We do not just hand out ready made answers here at the Ranch. Tell us what you think and we will help point you in the right direction.
As a first step, can you tell us, in plain english (forget code and algorithms for now) how you would solve this problem logically?
PS. This will be better suited on the beginners forum. Moving..