please check my code:
// memset
#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#define max 1000000007
using namespace std;
void display(int arr[],int n);
int findSum(int n);
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen(“testCase1.txt”,“r”,stdin);
freopen(“output.txt”,“w+”,stdout);
#endif
// while(true)
// {
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
map<int,int> hashWin;
for(int i=0;i<n;i++)
{
hashWin[arr[i]]=0;
}
int sum=0;
int left=0,right=0;
int prevLeft=-1;
hashWin[arr[0]]=1;
while(left<n && right<n)
{
// cout<<"the value is: "<<(arr[right+1]==arr[right]);
while(right<n && hashWin[arr[right]]<=1)
{
if(right==n-2 && hashWin[arr[right+1]]>0)
break;
// cout<<“hello”<<endl;
right++;
if(right<n)
hashWin[arr[right]]++;
}
// cout<<"right : “<< right<<” left: "<<left<<endl;
int len;
if(right==n)
len=right-1-left+1;
else {
len=right-left+1;
prevLeft=right;
}
sum+=findSum(len);
// prevLeft=right;
// cout<<"prevLeft: “<<prevLeft<<” left: "<<left<<endl;
if(left==prevLeft)
sum–;
while(left<n && left<right)
{
hashWin[arr[left]]–;
left++;
}
// cout<<“in while loop”<<endl;
// cout<<"right = "<<right<<endl<<"left = “<<left<<endl;
}
cout<<sum<<endl;
//}
// cout<<findSum(4)<<endl;
#ifndef ONLINE_JUDGE
cerr << “Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n\n”;
#endif
}
void display(int arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<” ";
}
cout<<endl;
}
int findSum(int n)
{
int sum=0;
int i=n+1;
int j=1;
while(i–)
{
// cout<<"i = “<<i<<” j = "<<j <<endl;
sum+=ij;
sum%=max;
j++;
}
// cout<<"sum is: "<<sum<<endl;
// int sum=(n(n+1))/2;
return sum;
}