Python | Easy | Explanation
When 2 robots collide, they just pass through each other. We dont have to worry about changing the direction of each robot when they collide. So we can say that
When 2 robots collide, they just pass through each other. We dont have to worry about changing the direction of each robot when they collide. So we can say that if a robot is moving right, then after d seconds it would have moved: its current position + d. If the robot is moving left, then after d seconds it would have moved: its current position - d.\nTherefore, we just need to check which position the robot is moving in and add or subtract d with its value.\n\n<span style="font-size:17px; font-weight:600">To find the sum of each pair we can do the following:</span>\nWe first need to sort the array. We can then notice that for any index i, its contribution to the sum would be: \n->  &nbsp; &nbsp; <span style="font-weight:600;">(nums[i] - nums[i-1]) + (nums[i] - nums[i-2]) + ... + (nums[i] - nums[0]).</span>\nThe above formula can be converted to:\n-> &nbsp;&nbsp;&nbsp;&nbsp;<span style="font-weight:600">(i * nums[i]) - sum(nums[0] to nums[i-1])</span>\nWe can keep a variable `s` that will keep track of the sum of the elements of the array upto the i<sup>th</sup> index. The contribution of the i<sup>th</sup> index to the sum can be calculated as below:\n```python\ns = 0 \nfor i in range(len(nums)):\n    ans += ((i * nums[i]) - s))\n    s += nums[i]\n```\n\nTime Complexity: O(nlogn) - For sorting the array.\nSpace Complexity: O(n) - For sorting (python internally uses timsort which takes O(n) extra space in the worst case).\n\n```\nclass Solution:\n    def sumDistance(self, nums: List[int], s: str, d: int) -> int:\n        for i in range(len(s)):\n            if s[i] == \'L\':\n                nums[i] -= d\n            else:\n                nums[i] += d\n                \n        ans = 0\n        nums.sort()\n        mod = 10 ** 9 + 7\n        s = 0\n        \n        for i in range(len(nums)):\n            ans += (nums[i] * i - s)\n            s += nums[i]\n            ans %= mod\n            s %= mod\n        \n        return ans % mod\n```
['Python3']