The requirements are really unclear here. I have added some comments in the code.