Subarrays with distinct elements Java

I am getting timed out for 2 test cases
Lemme know some alternate method, since this has too much time complexity

Hi,
see you can use here another simpler approach that you can make a arraylist in which you will keep a record that whether a particular number is repeated or not, then you will go on each and every element of the array and use one more loop inside that loop(j) for subsets and check that if j<n and do arraylist contains that jth element if yes then come out of the inside loop else keep on incrementing j and adding that jth number in arraylist.
Then 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 . (This is because the possible lengths of the subarrys will be 1,2,3,…till j-i+1,So sum will be the above formula.)
So when you will come out of the inside loop put ans=ans+(the above formula) and simply increment i.

Hi @justformyntra8 as you are not responding to the thread, I am marking your doubt as resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “Ask Doubt” section, when your doubt is resolved.

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.