Try asking an internet search engine for an algorithm for solving that problem. When you have the algorithm, then try writing code to solve it.
If you have problems writing the code, post the code...