No change of directions required by robots.  Sorting (O(n log n))
Intuition\n Describe your first thoughts on how to solve this problem. \nObservation - If two robots collide, direction changes but in actual - 1st robots becom
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Observation** - If two robots collide, direction changes but in actual - 1st robots becomes 2nd robots. Thats it!\n\nSince `d` is order of 1e9 , we can\'t think of postion of robots after every second, rather calculate where robots will be after `d` seconds. \n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWhen array is sorted and elements are distinct when we take sum of absolute difference of pairs each element in the i\u2019th position is added \u2018i\u2019 times and subtracted \u2018n-1-i\u2019 times. \n\nFor example in {1,2,3,4} element at index 2 is arr[2] = 3 so all pairs having 3 as one element will be (1,3), (2,3) and (3,4), now when we take summation of absolute difference of pairs, then for all pairs in which 3 is present as one element summation will be = (3-1)+(3-2)+(4-3). We can see that 3 is added i = 2 times and subtracted n-1-i = (4-1-2) = 1 times. \n\nThe generalized expression for each element will be sum = sum + (i*a[i]) \u2013 (n-1-i)*a[i]. \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n log n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n    int sumDistance(vector<int>& nums, string s, int d) {\n        \n        long long n = nums.size();\n        long long M = 1e9+7;\n    \n        for(int i=0; i<n; i++){\n            if(s[i] == \'R\')\n                nums[i] += d;\n            else\n                nums[i] -= d;\n        }\n        \n        sort(nums.begin(), nums.end());\n        \n        long long ans = 0;\n        \n        for (long long i=n-1; i>=0; i--){\n            long long x = i * nums[i];\n            long long y =  (n-1-i) * nums[i];\n            \n            ans = (ans%M + ((x%M - y%M))%M)%M;\n            if(ans < 0)\n                ans += M;\n        }\n        \n        return ans%M;\n        \n    }\n};\n```
['C++']