I am getting correct answer for the sample input as well as some of the inputs I tried.
Code - https://ide.codingblocks.com/s/261545
P.S - I have written my logic and algo in comments in the code itself.
It is giving wrong answer on submission. I am unable to figure out what is the problem or where it is failing. Please help me in getting it working or pointing out the case where it may fail.