Subarray with distinct element hashing

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

int main()
{
int n;
cin >> n;
unordered_map<int, int> s;
int ans = 0;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int j = 0;
int i = 0;
while (i < n)
{
//Starting from ith index find the largest unique elements subarray
for (j = i; j < n; j++)
{
if (s.find(arr[j]) == s.end())
{
s.insert(make_pair(arr[j], j));
}
else
{
j = j - 1;
break;
}
}
//Sum of all uniqe element subarrays from i to jth index
ans += ((j - i + 1) * (j - i + 2)) / 2;
//Since we have already calculated the sum of all the possible subarrays starting from index i
s.erase(arr[i]);
i++;
}
cout << ans << endl;
}

Not getting correct answer what is wrong in logic?

Save your code on ide.codingblocks.com and then share its link.

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.