Dsa
Two Pointers
Hard
Racing Cars Collision
You are given a list of cars represented by their positions on a straight line and their directions ('L' for left and 'R' for right). Your task is to determine the final positions of the cars after all collisions have been resolved. A collision occurs when two cars moving towards each other meet at the same position. When a collision occurs, the car moving left is removed, and the car moving right continues.
Input format:
The first line contains an integer n (1 ≤ n ≤ 10^6) - the number of cars.
The second line contains n space-separated pairs of integers, where each pair consists of the position and the direction of a car.
Output format:
Output the final positions of the cars as a space-separated list of integers.
Example:
Input:
5
1 R
2 L
3 R
4 R
5 L
Output:
1 3 4
Explanation:
The cars at positions 1 and 2 collide, removing the car at position 2. The car at position 3 stays. The car at position 4 stays. The car at position 5 collides with the car at position 4 and is removed.
Key concepts
two_pointerssimulationgreedy
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now