Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Can we use the prefix sum here?
For each number x, collect all the indices where x occurs, and calculate the prefix sum of the array.
For each occurrence of x, the indices to the right will be regular subtraction while the indices to the left will be reversed subtraction.