First you need to come up with the algorithm to solve the problem.
Take a paper and pencil and figure out how to solve the problem, or ask google.

When you have an algorithm for solving the...