Sbarrays with distinct elements

failing 2 testcases

Code:
#include<bits/stdc++.h>
using namespace std;
int main(){

vectorv1;
vectorv2;
long int n;
cin>>n;
for(long int i=0;i<n;i++){
int temp;
cin>>temp;
v1.push_back(temp);

}
long long int sum =0;
for(long int i=0;i<n;i++)
    {
        auto itr = find(v2.begin(),v2.end(),v1[i]);
        if(itr == v2.end()){
            v2.push_back(v1[i]);
             }
        else{

        for(long int k=1;k<=v2.size();k++){
        sum = sum + ((k*(v2.size()+1-k))%1000000009);
        sum = sum%(1000000009);
        }
        v2.clear();
        i--;

        }

    }

        for(long int k=1;k<=v2.size();k++){
        sum = sum + ((k*(v2.size()+1-k))%1000000009);
        sum = sum%(1000000009);
        }


cout<<(sum%(1000000007))<<"\n";
v2.clear();

return 0;
}

Hi

Please explain the logic of your code, not bale to understand this.

Hint: you just need to calculate distinct parts in array and calculate sum of lengths.

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

int main()
{
ll n,i;
cin>>n;
vector v(n),a(1000006,-1);
for(i=0;i<n;i++)
cin>>v[i];
ll j=-1,ans=0;
for(i=0;i<n;i++)
{

    j=max(j,a[v[i]]);
    ll k=(i-j);
    k=k*(k+1)/2;
    a[v[i]]=i;
    ans=(ans+k)%1000000007;
    
   // cout<<ans<<"\n";
}
cout<<ans<<"\n";

}

If you need explanation for my code do tell me.

Hit like if you get it!
Cheers!

Hi,
As you are not replying to this thread i’m marking this as resolved you can reopen it later if you still have doubt.

Cheers!