✅ Explained Solution - All testcases ✅ O(n log n)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n In the first loop, we convert the integer positions \'nums[i]\' of the r
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n In the first loop, we **convert the integer positions \'nums[i]\' of the robots to \'long\'** for precision and add/subtract the \'d\' distance depending on their direction (\'R\' or \'L\').\n\n**Sorting:** Then, we sort this array longnum. This is a crucial step. Once sorted, the array represents the final positions of all robots on the number line, from left to right, d seconds later. Any collisions that might happen are already taken into account because the directions of robots after a collision do not affect the final position of any robot.\n\n**Sum Calculation:** Next, in another loop, res calculates the sum of all pairwise distances. The formula (1L + i + i - n) * longnum[i] calculates the sum of distances of robots from a particular robot, where \'i\' is the index (or position) of this robot on the sorted array (or the sorted number line).\n\n*This works because in the sorted array, every robot to the left of a given robot at longnum[i] will have a negative distance (since they are to the left), and every robot to the right will have a positive distance from longnum[i]. Since the array is 0-based, there will be \'i\' number of robots to the left and \'n - i - 1\' robots to the right. But the formula uses \'n - i\' because it includes the robot at position \'i\' also.\n*\n**Modulo Operation:** The modulo operation keeps the sum in the range of the prescribed limit 1_000_000_007 to keep the number within the \'int\' range and to meet the problem\'s requirements.\n\n**In summary**, the code correctly computes the sum of all pairwise distances between robots \'d\' seconds after they have been given commands to move, taking into account any collisions. It does this efficiently by sorting the final positions of robots and calculating pairwise distances in O(n). The overall time complexity is O(n log n) due to the sorting operation, and space complexity is O(n) as extra space is used for storing \'longnum\'.\n\n# Complexity\n- Time complexity: **O(n log n)**\n\nThe two major operations that impact the time complexity of the code are:\nLooping over the input array nums to fill longnum: Which is O(n), where n is size of the given array.\nSorting of the longnum array: Which in worst-case scenario is O(n log n), considering the sort function uses a comparison-based sorting algorithm like quick sort or merge sort.\nSo, the time complexity will be dominated by the sorting operation, hence, it will be O(n log n).\n\n- Space complexity: **O(n)** \n\nThe space complexity is dictated by the additional space used apart from the input. In this case, it\'s the longnum array that has the same size as the input array nums, so the space complexity is O(n) as we are storing n extra long integer elements. Other variables used are of constant space and don\'t impact the overall complexity.\n\n# Code\n```\nclass Solution {\n    public int sumDistance(int[] nums, String s, int d) {\n      int n = nums.length, mod = 1_000_000_007;\n      long longnum[] = new long[n];\n        for(int i = 0; i < n; ++i)\n            longnum[i] = (long)nums[i] + (s.charAt(i) == \'R\' ? d : -d);\n        Arrays.sort(longnum);\n        long res = 0;\n        for(int i = 0; i < n; ++i)\n            res = (res + (1L + i + i - n) * longnum[i] ) % mod;\n        return (int)(res+mod)%mod;   \n    }\n}\n```
['Java']