How to solve the TORN PAGES question?

Please help me solve the TORN PAGES problem.

I made a few submissions, but got a WA. Please help me solve the problem. Here’s the link to my code.

@dhruvsomani - I have tried this question and I am also getting wrong answer, but I could tell you that your code will fail for test cases when S=4 & 64 and possible others…Check out my solution for reference but it is not fully correct

it is a tricky one !!

i didn’t know python so, i cant suggest anything but i can tell u the corner cases which your code isn’t passing :slight_smile:

56(answer is -1 but your code gives 17 because may be u are not checking that the output page exists or not )!