How does ((j-i+1)*(j-i+2))/2 this formula works

As in the hint it is given that to calculate the sum of lengths of subarrays of an array of distinct elements starting from i index up to j index is = ( ( j-i+1 ) * ( j-i+2 ) )/2

if we apply this on an array indexed from 0 to 2
and the array is lets say arr[] = { 1 ,2,3 }

value we get from the formula is 6 but the actual value is 10 also given in the sample output,

for this condition ans is 6

for whole array ans is 10

to get ans for complete array you have to run a loop where i varies from 0 to n-1

in this case
from 0 to 2 ans is 6
from 1 to 2 ans is 3
from 2 to 2 ans is 1
so total ans is 10

i hope you got the point

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.