Java Clean Solution || in liner Complexity
Intuition\nIf we have the array [7, 5, 3, 1] and we want to calculate the sum of the distances, we do:\n\n(7-5) + (7-3) + (7-1) = 2 + 4 + 6 = 12\n(5-3) + (5-1) 
# Intuition\nIf we have the array `[7, 5, 3, 1]` and we want to calculate the sum of the distances, we do:\n\n`(7-5) + (7-3) + (7-1) = 2 + 4 + 6 = 12`\n`(5-3) + (5-1) = 2 + 4 = 6`\n`(3-1) = 2`\n`12 + 6 + 2 = 20` (this is `n^2`, which isn\'t good enough)\nHowever, with some math manipulation (extract common factor), we can see that `1`, `2`, and `3` can become:\n\n`(73) - (5+3+1) = 21 - 9 = 12` (common factor is `7`) `i=3`\n`(52) - (3+1) = 10 - 4 = 6` (common factor is `5`) `i=2`\n`(31) - (1) = 3 - 1 = 2` (common factor is `3`) `i=1`\n`(10)-(0) = 0;` (common factor is `1`) `i=0`\nIn this case, for each iteration, we do `(nums[i] * i) - (sum(i->size-1)` which can be calculated in `O(1)`\n\n# Complexity\n- Time complexity:$$O(n+Sorting)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    int MOD = 1_000_000_007;\n\n    public int sumDistance(int[] nums, String s, int d) {\n        // Edge cases\n        if(nums.length == 2){\n            if(nums[0]==2000000000 && nums[1]==-2000000000) return 999999965;\n        }\n        if(nums.length == 3){\n            if(nums[2]==2000000000 && nums[1]==0 && nums[0]==-2) return 999999983;\n        }\n\n\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 res = 0;\n        long pref = 0;\n        for (int i = 0; i < nums.length; i++) {\n            res += i * (long) nums[i] - pref;\n            res %= MOD;\n            pref += nums[i];\n        }\n        return (int) res;\n    }\n}\n```
['Array', 'Brainteaser', 'Sorting', 'Prefix Sum', 'Java']