Have you tested on the maximal N = 1e6 as stated in the problem description? Your solution seems to be O(N*N) so it will be really slow.
N = 1e6
Hence “simple” not “efficient‘ 😉
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.