2 testcase not cleared

please tell the problem in the code

hello @Akshita99
there is logical error in ur code.
u are assuming that if some range let say i…j is having all distinct elemets then they will never occur in some other range but it is not correct.

The solution is based on the fact that if we know all elements in a subarray arr[i…j] are distinct, sum of all lengths of distinct element subarrays in this subarray is ((j-i) * (j-i+1))/2 .
How? the possible lengths of subarrays are 1, 2, 3,……, j – i +1. So, the sum will be ((j – i ) * (j – i +1))/2.
We first find largest subarray (with distinct elements) starting from first element. We count sum of lengths in this subarray using above formula. For finding next subarray of the distinct element, we increment starting point, i and ending point, j unless (i+1, j) are distinct. If not possible, then we increment i again and move forward the same way.

int sumOflength(int *arr, int n) {
unordered_set<int> s;
int j=0, ans=0;
for(int i=0;i<n;i++) {
    while(j<n and s.find(arr[j])==s.end()) {
        s.insert(arr[j]);
        j++;
    }
    ans+= ((j-i)*(j-i+1))/2;
    s.erase(arr[i]);
}
return ans;

}