Below code has passed all test cases on codeforces ,but t is showing bad_alloc here

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main() {
ll n,x;
cin>>n;
double sum=0;
//unique contains the all subarray ending at that position and the count of unique elemnts of all subarrays
vector unique(n+1),last_occurance(1000006);
for(ll i=1;i<=n;i++){
cin>>x;
unique[i]=unique[i-1]+i-last_occurance[x];
last_occurance[x]=i;
sum+=(unique[i]);
}
cout<<fixed<<setprecision(6)<<(2*(sum-n)+n)/(n*n);

return 0;

}

Share your code via ide.codingblocks.com after removing compilation errors.

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.