Practice Sum of Distances
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
You are given a 0-indexed integer array nums. There exists an array arr of length nums.length, where arr[i] is the sum of |i - j| over all j such that nums[j] == nums[i] and j != i. If there is no such j, set arr[i] to be 0. Return the array arr. Example 1: Example 2: Constraints: Note: This question is the same as 2121: Intervals Between Identical Elements.
Source: Sum of Distances on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
nums = [1,3,1,1,2]
[5,0,3,4,0]
# Example 2
nums = [0,5,3]
[0,0,0]
Constraints
Related LeetCode Problems
Practice more problems similar to Sum of Distances.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.