I am getting wrong answer on submitting the Random Query problem on codeforces

Here is my code :

#include<bits/stdc++.h>
#define ll long long
using namespace std;

ll a[1000006] = {0};
ll last_occured[1000006] = {0};
ll dp[1000006] = {0};

int main()
{
#ifndef ONLINE_JUDGE
freopen(“input.txt”,“r”, stdin);
freopen(“output.txt”,“w”, stdout);
#endif

    int n; cin >> n;

    for(ll i = 1; i <= n; i++) {
        cin >> a[i];
    }

    memset(last_occured,0,sizeof(last_occured));
    memset(dp,0,sizeof(dp));

    dp[0] = 0;
    double sum = 0.0;

    for(ll i = 1; i <= n; i++) {

        dp[i] = dp[i-1] + (i - last_occured[a[i]]);
        last_occured[a[i]] = i;

        sum += dp[i];

    }

    double result = 0.0;
    result = (2*(sum - n) + n)/(n*n*1.0);

    cout << fixed << setprecision(6) << result << endl;


return 0;

}

Hello @deep_01,

Change int n to ll int, because in the below statement n*n would overflow.

result = (2*(sum - n) + n)/(nn1.0);

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.