FastPrepFastPrep
Problem Brief

Maximum Riders in Car

FULLTIMEOA

There are n riders and one car with infinite capacity. Each rider i is comfortable riding in the car only if the number of other riders also in the car is between comfort[i][0] and comfort[i][1] (inclusive). If this condition is not met, the rider will not enter the car.

All riders decide simultaneously — a valid group is a subset of riders where every rider in the group is comfortable with the group size minus one.

Return the maximum number of riders that can be in the car at the same time.

1Example 1

Input
comfort = [[1, 3], [2, 4], [0, 2], [1, 3]]
Output
3
Explanation

With k=3 riders, each needs to be comfortable with 2 others. Riders 0 (1–3), 1 (2–4), and 2 (0–2) all include 2 in their range → valid group of 3.
With k=4, each would need 3 others. Only riders 0, 1, and 3 cover 3 — only 3 riders qualify, not 4.

2Example 2

Input
comfort = [[0, 2], [0, 2], [0, 2]]
Output
3
Explanation

With all 3 riders, each is comfortable with 2 others (2 ∈ [0, 2]). All 3 can ride together.

3Example 3

Input
comfort = [[2, 3], [2, 3]]
Output
0
Explanation

k=1 requires 0 others; k=2 requires 1 other. Neither 0 nor 1 falls in [2, 3] for any rider, so no valid group exists.

4Example 4

Input
comfort = [[0, 0]]
Output
1
Explanation

The single rider is comfortable with 0 others. They ride alone.

Constraints

Limits and guarantees your solution can rely on.

  • 1 ≤ n ≤ 105
  • 0 ≤ comfort[i][0] ≤ comfort[i][1] ≤ n
public int maxRiders(int[][] comfort) {
    // write your code here
}
Input

comfort

[[1, 3], [2, 4], [0, 2], [1, 3]]

Output

3

Sign in to submit your solution.