nums
.nums
and populate the HashMap with the indices of each number.result
to store the sum of distances for each index.nums
again and for each index i
, use the prefix sum to calculate the sum of distances to the left and right.result
array.result
array.