my code does not pass one of the testcase. why???
Subarrays with distinct element doubt
check out this
int sumoflength(int arr[], int n)
{
// For maintaining distinct elements.
unordered_set<int> s;
// Initialize ending point and result
int j = 0, ans = 0;
// Fix starting point
for (int i=0; i<n; i++)
{
// Find ending point for current subarray with distinct elements.
while (j < n && s.find(arr[j]) == s.end())
{
s.insert(arr[j]);
j++;
}
// Calculating and adding all possible length subarrays in arr[i..j]
ans += ((j - i) * (j - i + 1))/2;
// Remove arr[i] as we pick new starting point from next
s.erase(arr[i]);
}
return ans;
}
sir my code fail one of the testcase. correct me sir
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.