Java, sorting + linear scan
Intuition\nThe first observation is we can ignore meetings of the robots because the meeting robots will continue their journey and we don\'t care about the rob
# Intuition\nThe first observation is we can ignore meetings of the robots because the meeting robots will continue their journey and we don\'t care about the robot ids.\n\nThe second trick is to avoid `O(n^2)` complexity for sum distance of all pairs.\n\nEplanation of the line `long curr = i * (long)nums[i] - (n - 1 - i) * (long)nums[i]`\n\n*Note1*: let\'s use long to avoid integer overflow.\n\n*Note2*: Let\'s take a look on the example `{1,2,3}`. All pairs sums will be:\n`(1,2) + (1,3) + (2,3)` = `2 - 1 + 3 - 1 + 3 - 2`. So, we use each number in the array `n - 1` times. We substtract the number if it is less in the pair `(i, j)`, once array is sorrted it means `i` number will be bigger (added to sum) `i` times and less (substructed from the sum) `n - 1 - i` times.\nGeneral formula per number: `nums[i] * i - nums[i] * (n - 1 - i)`;\n\n*Note3*: the array is sorted, so we always substtruct big numbers less then add big number. So we don\'t car about `abs`. The example arr `{-2, -1}` is `(-2, -1) = -2 * 0 - -2 + -1 - -1 * 0 = 2 - 1 = 1`\n\n# Complexity\n- Time complexity: O(nlogn)\n- Space complexity: O(1)\n\n# Code\n```\nclass Solution {\n    public int sumDistance(int[] nums, String s, int d) {\n        final int mod = 1_000_000_007;\n        for (int i=0; i<nums.length; i++) {\n            nums[i] += d * (s.charAt(i) == \'R\' ? 1 : -1);\n        }\n        \n        Arrays.sort(nums);\n        long sum = 0;\n        int n = nums.length;\n        for (int i=0; i<n; i++) {\n            long curr = i * (long)nums[i] - (n - 1 - i) * (long)nums[i];\n            sum += curr;\n            sum %= mod;\n        }   \n        \n        return (int) sum ;\n    }\n}\n```
['Sorting', 'Java']