if n is 10^4 then should we not able to solve it in O(N^2) if yes then why last submission failed in test case 6 where i think my time complexity is O(N^2)
Time complexity
10^4 with O(n^2) means 10^8, while one second limit means 10^6 operations.
refer to this, here I have explained all the corner cases, and the entire algo for this question. You would surely find it helpful