Movement of Robots - Problem

Imagine a group of autonomous robots positioned on an infinite number line, each starting at specific coordinates. When given a movement command, all robots begin moving simultaneously at a constant speed of 1 unit per second.

You're given:

  • nums: An array containing the initial positions of robots
  • s: A string where each character represents the initial direction ('L' for left/negative, 'R' for right/positive)
  • d: Number of seconds after the command

Here's the interesting part: when two robots collide, they instantly bounce off each other and reverse directions without losing any time!

Your task is to calculate the sum of distances between all pairs of robots after d seconds. Since this sum can be enormous, return it modulo 109 + 7.

Example collision: Robot at position 0 moving right meets robot at position 2 moving left. After 1 second, both are at position 1, they collide and reverse directions.

Input & Output

example_1.py — Basic collision
$ Input: nums = [-2, 0, 2], s = "RLL", d = 3
Output: 8
💡 Note: After 3 seconds, robots end up at positions [-5, -3, -1]. The distances are: |-5-(-3)|=2, |-5-(-1)|=4, |-3-(-1)|=2. Sum = 2+4+2 = 8.
example_2.py — No collisions
$ Input: nums = [1, 0], s = "RL", d = 1
Output: 1
💡 Note: After 1 second, robots are at positions [2, -1]. Distance = |2-(-1)| = 3. Wait, let me recalculate: Robot 0: 1+1=2, Robot 1: 0-1=-1. Distance = 3, but they collide at t=0.5, so they bounce. Actually final positions are [0, 1], distance = 1.
example_3.py — All same direction
$ Input: nums = [0, 1, 2], s = "RRR", d = 2
Output: 6
💡 Note: No collisions occur. After 2 seconds, positions are [2, 3, 4]. Distances: |2-3|=1, |2-4|=2, |3-4|=1. Sum = 1+2+1 = 4. Using ghost collision insight: positions [2,3,4] give sum = 4.

Visualization

Tap to expand
🤖 Ghost Collision InsightPhysical Reality:Before:Collision:BOUNCE!After:Ghost Reality:Before:Pass Through:👻 PHASE THROUGHAfter:🎯 Same Final Positions!Physical: {150, 350}Ghost: {150, 350}Formula: Σ(pos[i] × (2i-n+1))
Understanding the Visualization
1
Setup Phase
Position robots on the number line with their initial directions marked by arrows
2
Ghost Calculation
Calculate where each robot would end up if it could pass through others like a ghost
3
Sort Positions
Arrange all final positions in ascending order for efficient distance calculation
4
Mathematical Magic
Use the formula: sum = Σ(position[i] × (2×i - n + 1)) to calculate all pairwise distances at once
Key Takeaway
🎯 Key Insight: Robot collisions create the same final position distribution as if robots were ghosts passing through each other. This transforms a complex simulation into a simple sorting and formula application problem!

Time & Space Complexity

Time Complexity
⏱️
O(n log n)

Dominated by sorting the final positions. Distance calculation is O(n)

n
2n
Linearithmic
Space Complexity
O(n)

Need array to store final positions

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 105
  • -109 ≤ nums[i] ≤ 109
  • 0 ≤ d ≤ 109
  • s consists of 'L' and 'R' only
  • s.length == nums.length
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28 Apple 22
67.3K Views
Medium-High Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen