Kindly tell the mistake and modify the code

first thing you have to do is increase size of array according to constraints 10^6

also you code time complexity is high O(n^2)
which will lead to TLE

try to do some precomputation