C++ Easy to understand, steps given, TC(nlogn), SC O(1), not prefixsum
Steps\n- There won\'t be any effect of collision, you can see that by  taking example and doing yourself\n- So, the main part was to find, Sum of absolute diffe
# Steps\n- There won\'t be any effect of collision, you can see that by  taking example and doing yourself\n- So, the main part was to find, Sum of absolute differences of all pairs in modified vector of positions\n- For this refer to this gfg post https://www.geeksforgeeks.org/sum-absolute-differences-pairs-given-array/\n\n# Complexitymath\n- Time complexity:\n-- O(n*logn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n-- O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n//https://www.geeksforgeeks.org/sum-absolute-differences-pairs-given-array/\n//An efficient solution for this problem needs a simple observation. \n//Since array is sorted and elements are distinct \n//when we take sum of absolute difference of pairs \n//each element in the i\u2019th position is added \u2018i\u2019 times \n//and subtracted \u2018n-1-i\u2019 times. \n\n//SUMMARY\n//ith element will be added i time and subtracted (n-1-i) times\n\n    int sumDistance(vector<int>& nums, string s, int d) {\n        int n = nums.size();\n        long long mod = 1e9+7;\n\n        //modify the positions O(n)\n        for(int i=0;i<n;i++) {\n            if(s[i]==\'R\') nums[i]+=d;\n            else nums[i]-=d;\n        }\n\n        if(n==2) return (abs(nums[0]-nums[1]))%mod;\n\n        //sorting O(nlogn)\n        sort(nums.begin(), nums.end());\n        \n        long long ans = 0;\n\n        //O(n)\n        for(int i=0;i<n;i++) {\n            long long add = (long long)(i);\n            long long minus = (long long)(n-1-i);\n            long long final = ((add - minus)*(long long)(nums[i]))%mod;\n            ans = (ans + final)%mod;\n        }\n        return ans;\n    }\n};\n```
['Math', 'Sorting', 'C++']