Class Solution
-
- All Implemented Interfaces:
public final class Solution
2615 - Sum of Distances\.
Medium
You are given a 0-indexed integer array
nums
. There exists an arrayarr
of lengthnums.length
, wherearr[i]
is the sum of|i - j|
over allj
such thatnums[j] == nums[i]
andj != i
. If there is no suchj
, setarr[i]
to be0
.Return the array
arr
.Example 1:
Input: nums = 1,3,1,1,2
Output: 5,0,3,4,0
Explanation:
When i = 0, nums0 == nums2 and nums0 == nums3. Therefore, arr0 = |0 - 2| + |0 - 3| = 5.
When i = 1, arr1 = 0 because there is no other index with value 3.
When i = 2, nums2 == nums0 and nums2 == nums3. Therefore, arr2 = |2 - 0| + |2 - 3| = 3.
When i = 3, nums3 == nums0 and nums3 == nums2. Therefore, arr3 = |3 - 0| + |3 - 2| = 4.
When i = 4, arr4 = 0 because there is no other index with value 2.
Example 2:
Input: nums = 0,5,3
Output: 0,0,0
Explanation: Since each element in nums is distinct, arri = 0 for all i.
Constraints:
<code>1 <= nums.length <= 10<sup>5</sup></code>
<code>0 <= numsi<= 10<sup>9</sup></code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-