
Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
For each unique value found in the array, store a sorted list of indices of elements that have this value in the array.
One way of doing this is to use a HashMap that maps the values to their list of indices. Update this mapping as you iterate through the array.
Process each list of indices separately and get the sum of intervals for the elements of that value by utilizing prefix sums.
For each element, keep track of the sum of indices of the identical elements that have come before and that will come after respectively. Use this to calculate the sum of intervals for that element to the rest of the elements with identical values.