Car Fleet
There are n cars traveling toward the same destination.
You are given two integer arrays:
- target — the destination position.
- position[i] — the starting position of the ith car.
- speed[i] — the speed of the ith car.
A car cannot pass another car. If a faster car catches up to a slower car, they form a car fleet and travel together at the slower car's speed.
Return the number of car fleets that will arrive at the destination.
Example 1
Input
12 5 10 8 0 5 3 2 4 1 1 3
Output
3
Explanation
The cars form three fleets that reach the destination.
Example 2
Input
10 3 3 5 7 3 2 1
Output
1
Explanation
All three cars eventually catch up and form a single fleet before reaching the destination.
Constraints
Hints:
Hint 1
Calculate the time required for every car to reach the destination:
time = (target - position[i]) / speed[i]
Hint 2
Sort the cars by their starting positions from closest to the destination to farthest.
Author & Technical Reviewer
Technically reviewed by: ExamAdda Technical Review Team
Technical Reviewers, ExamAdda
Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.
Published
Aug 10, 2026
Last updated
Aug 10, 2026
Content Verification Methodology
Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.
Expected Output