Important: I decoded the Logic.
I read your description that you need a code for a coding question in C++.
I am one of the Finest Coders in India with a lot of achievements I bagged in the past years in various Prestigious Coding Competitions like ACM ICPC Regionals under Top 15, Google Hash Code (1st in South India), CodeChef Snackdown Elimination under Top 100, etc., and rewarded many prize monies for my Exceptional Knowledge and Skills over various Programming Languages like C, C++, JAVA, Python, Dart etc.
The project duration may change with your requirements.
Do Hire ME!! :)
Please do message me over the chat to discuss.
Thanks & Regards,
Ch Jagadeesh