Easy Code with complete Explanation
Intuition and Approach\nThe main thought to solve this Question is u don\'t need to worry about the collision and change of direction. \n# Explanation (why coll
# Intuition and Approach\nThe main thought to solve this Question is u don\'t need to worry about the collision and change of direction. \n# Explanation (why collision don\'t affect the Ans)\nTwo cases of collision is given:-\n(i) If they collide and passes each other or can they swap their position on number line then in this case their direction is not changed so this cases is already handled without doing anything(given in Q).\n(ii) They collide because of same position on number line then in this case we have to change their direction but if we still didn\'t change the direction then still we got the same ans because later we have to calculate the sum of differences between element that\'s why it won\'t affect the ans if we have to return the final position of all element then definitly we got the wrong ans.\nlet\'s take the same example 1 of question after 1 sec position are [-1,-1,1] and acc to question direction changed to "LRL" and after next sec position are [-2,0,0] but if we don\'t change the direction then postion are [0,-2,0] so we can say that position of robot on number line are changed in this case which doesnot affect the ans beacuse we have to calculate the sum of differences in their position.\n# Next Step\nAs the length of nums array is 10^5 so we can\'t use n^2 time complexity to find the difference between the element.\n\nSo.....we use *Prefix Sum* approach to get the ans.\n\nLet\'s take an example \ngiven arr=[a1,a2,a3,a4] \nQ.we have to find sum of distance of a4 with all elements before it\n\nAns:- (a4-a1)+(a4-a2)+(a4-a3) == (a4+a4+a4)-(a1+a2+3)\n4 element in array so index of a4 is 3 so (3*a4) and a1+a2+a3 is prefix sum of all the previous element the ans is = (index_of_a4) * a4 - prefix_sum.\nSo, this approach is we are going to use to solve the remain part of Q.\nBut make sure this approach is only work when the array is sorted bcz we multiply the element with its index if you didn\'t sort the array u will get the wrong ans.if u r still not getting why to sort array u just take the first example- final postion after d sec is [-3,-1,1] just do the next remain steps to get the ans after sorting of array and without sorting of array u will get ur ans why to sort.\n\n# Complexity\n- Time complexity:O(nlogn)\n\n- Space complexity:O(1)\n\n# Code\n```\nclass Solution {\n    public int sumDistance(int[] nums, String s, int d) {\n        for(int i=0;i<nums.length;i++){\n            if(s.charAt(i)==\'R\') nums[i]+=d;\n            else nums[i] -=d;\n        }\n        Arrays.sort(nums);\n        int MOD = 1000000007;\n        long ans = 0,prefix_Sum = 0;\n        for(int i=0;i<nums.length;i++){\n            ans = (ans + i*(long)nums[i] - prefix_Sum)%MOD;\n            prefix_Sum += nums[i]%MOD;\n        }\n        return (int)ans;\n    }\n}\n```
['Sorting', 'Prefix Sum']