FastPrepPassing Cars

Passing Cars

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Each value in directions describes a car's travel direction along a two-way road: 1 travels east and 0 travels west. Return the number of pairs (i,j) with i < j, directions[i] == 1, and directions[j] == 0.

Function

countPassingCars(directions: int[]) → long

Examples

Example 1

directions = [1,0,1,0,0,1]return = 5

The first eastbound car passes three westbound cars and the second passes two.

Constraints

  • 0 <= directions.length <= 2 * 10^5.
  • Every value is 0 or 1.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public long countPassingCars(int[] directions) {
  // Write your code here.
}
directions[1,0,1,0,0,1]
expected5
Checking account…