prefixsum intution explained💯💡 || O(NlogN)
\n\n# Code\n\nclass Solution {\npublic:\n    int mod=1e9+7;\n    int sumDistance(vector<int>& nums, string s, int d) {\n        vector<long long int> ans;\n    
\n\n# Code\n```\nclass Solution {\npublic:\n    int mod=1e9+7;\n    int sumDistance(vector<int>& nums, string s, int d) {\n        vector<long long int> ans;\n        int n=nums.size();\n        for(int i=0; i<n; i++){\n            ans.push_back(nums[i]);\n        }\n        //we can ignore the collsions as the order of the positions is insignificant in this problem\n        for(int i=0; i<n; i++){\n            if(s[i]==\'L\'){\n                ans[i]=(ans[i]-d);\n            }\n            else{\n                ans[i]=(ans[i]+d);\n            }\n        }\n        long long int a=0;\n        long long int pref=0;\n        //imagine finding distance of a4 with all elements before it\n        //(a4-a1)+(a4-a2)+(a4-a3)---> (a4+a4+a4)-(a1+a2+3)---->i*a4-prefixsum\n        //this formula can be used only if the positions are sorted\n        sort(ans.begin(), ans.end());\n        for(int i=0; i<n; i++){\n            a+=i*ans[i]-pref;\n            a%=mod;\n            pref+=ans[i];\n        }\n        return a;\n    }\n};\n\n```
['C++']