Subarray with distinct elements

tell me the approach and logic for this question

hi @kumarakash121005,
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.

for implementation difficulties refer https://ide.codingblocks.com/s/661004

I am not able to understand it properly this soln is also on gfg

@kumarakash121005,
see the logic is simple till you dont get any duplicate element in the streak continue expanding else you start calculating answers for the elements in the window rom start till you dont remove the element which was causing duplicacy

Do a dry run for 1 2 3 2 4 5 you’ll get it

set ki jgh map nhi use kr skte kya

@kumarakash121005 yes u can

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.