Due to the time limit in the question, this question is to be done in O(n), but I can’t find any problem in my code, but its giving WA there. I have tried many test cases on my own. I am new to programming, so couldn’t understand the sliding window algo written in the comments.
Thanks in advance!
Here is my solution -> https://ideone.com/bg4CIF