it works correctly for sample test case, but gives wrong answer when I submit
Importance of time
@dhairya16 It was probably due to freopen() and you missed one case which I added.
You can have alook at this code .https://ide.codingblocks.com/s/244402
If your doubt is resolved please close this doubt