human
Subarrays with distinct elements | Returns sum of lengths of all subarrays with distinct elements ..
For maintaining distinct elements ..
Initialize ending point and result.
Fix starting point.
Calculating and adding all possible length subarrays in arr [ i . . j ].
Remove arr [ i ] as we pick new stating point from next.
Driven Code"
Solution in Python:
assistant
#Here is code to subarrays with distinct elements and the other steps required.
 def sumoflength ( arr , n ) :
	s = [ ]
	j = 0
	ans = 0
	for i in range ( n ) :
		while ( j < n and ( arr [ j ] not in s ) ) :
			s . append ( arr [ j ] )
			j += 1
		ans += ( ( j - i ) * ( j - i + 1 ) ) // 2
		s . remove ( arr [ i ] )
	return ans
if __name__ == " _ _ main _ _ " :
	arr = [ 1 , 2 , 3 , 4 ]
	n = len ( arr )
	print ( sumoflength ( arr , n ) )