EASY C++ solution using sorting and prefix sum
\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(1sorting)\n\n# Code\n\nclass Solution {\npublic:\n    int sumDistance(vector<int>& nums
\n\n# Complexity\n- Time complexity:\nO(nlogn)\n\n- Space complexity:\nO(1sorting)\n\n# Code\n```\nclass Solution {\npublic:\n    int sumDistance(vector<int>& nums, string s, int d) {\n        int n=nums.size();\n        long long ans=0,sum=0;\n        int mod=1e9+7;\n        //as we need only final position of robots irrespective of which robot;\n        for(int i=0;i<n;i++){\n            if(s[i]==\'L\')\n            nums[i]-=d;\n            else\n            nums[i]+=d;\n        }\n        //to calculate distance in nlogn time\n        sort(nums.begin(),nums.end());\n         \n         //effect of adding a new point xi will contribute as xi *i - (previous distance sum)\n        for(long long i=0;i<n;i++){\n            ans+=(nums[i]*i -sum)%mod;\n            ans%=mod;\n            \n            sum+=nums[i];\n            sum%=mod;\n        }\n\n       return ans;\n\n    }\n};\n```
['Math', 'Sorting', 'Prefix Sum', 'C++']