Fully explained C++ code || Beats 90% solutions || Easy to understand
Intuition\nThe main intuition is when 2 robots collide, it switches direction. but for the purpose of finding the pair wise distance, it is equivalent to not sw
# Intuition\n**The main intuition is when 2 robots collide, it switches direction. but for the purpose of finding the pair wise distance, it is equivalent to not switching the robots\' position and keep the same direction.**\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nTo solve this we need to keep in mind these 2 things -\n\n- Here a new vector is used of type `<long long>` because when we add or sub the distance after time `d`, then it may cause an overflow.\n- To calculate the pair wise distance sum we need to understand how prefix sum can be used:\n\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    So this formula can be used only if the numbers are sorted.\n\n**IF YOU LIKED THE EXPLANATION PLEASE DO UPVOTE IT**\n\nHere\'s a detailed explanation of the code:\n\n1. `int mod = 1e9 + 7;`: This line defines a constant integer `mod` with a value of 1e9 + 7. This value is used for modulo operations later in the code.\n\n2. `vector<long long> newNums;`: A new vector of type `long long` named `newNums` is declared. This vector will store the modified values of the input vector `nums`. Using `long long` ensures that large integers can be handled without causing overflow.\n\n3. The `for` loop iterates through the elements of the `nums` vector and copies each element to the `newNums` vector. This creates a deep copy of the input vector.\n\n4. The next `for` loop iterates through the `newNums` vector. For each element at index `i`, it checks the corresponding character in the string `s`. If `s[i]` is \'L\', it subtracts the value of `d` from `newNums[i`. If `s[i]` is anything other than \'L\' (i.e., \'R\'), it adds the value of `d` to `newNums[i]`.\n\n5. After these modifications, the `newNums` vector contains the elements of `nums` adjusted according to the operations specified in the string `s`.\n\n6. `sort(newNums.begin(), newNums.end());`: The `newNums` vector is sorted in ascending order.\n\n7. Two variables `distance` and `prefixSum` are declared and initialized to 0. These variables will be used to calculate the sum of distances between elements in the sorted `newNums` vector.\n\n8. Another `for` loop iterates through the elements of the sorted `newNums` vector. For each element at index `i`, it calculates the distance using the formula:\n   ```\n   distance = (distance + (1LL * i * newNums[i] - prefixSum)) % mod;\n   ```\n   - `1LL * i * newNums[i]` calculates the contribution of the current element to the distance.\n   - `prefixSum` is used to keep track of the cumulative sum of elements in the sorted vector.\n   - The result is updated in the `distance` variable, and it is calculated modulo `mod` to ensure the result remains within reasonable bounds.\n\n9. Finally, the function returns the calculated `distance`, which represents the sum of distances between the modified elements in the sorted vector, taking into account the operations specified in the string `s` and applying modulo `mod`.\n\nThis code efficiently calculates the desired sum of distances while handling potential integer overflow by using `long long` and modulo operations.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    int mod = 1e9 + 7 ;\npublic:\n    int sumDistance(vector<int>& nums, string s, int d) {\n        vector<long long> newNums ; // Create a new vector to store modified values\n        for(int i = 0; i<nums.size(); i++)\n            newNums.push_back(nums[i]) ;\n\n        for(int i = 0; i<newNums.size(); i++) {\n            if(s[i] == \'L\')\n// -2000000000 - 1000000000 can cause overflow if <long long> type not used\n                newNums[i] = newNums[i] - d ;\n            else\n// 2000000000 + 1000000000 can cause overflow if <long long> type not used\n                newNums[i] = newNums[i] + d ;\n        }\n        \n        sort(newNums.begin(), newNums.end()) ;\n        long long distance = 0, prefixSum = 0 ;\n        for(int i = 0; i<newNums.size(); i++) {\n// The formula calculates the distance and updates \'prefixSum\' for the next iteration.\n            distance = (distance + (1LL*i*newNums[i] - prefixSum)) % mod ;\n            prefixSum = (prefixSum + newNums[i]) % mod ;\n        }\n        return distance ;\n    }\n};\n```
['Array', 'Brainteaser', 'Sorting', 'Prefix Sum', 'C++']