Count the number of passing cars on the road.
In this challenge we can keep track of the number of cars travelling east, then every time we pass a car travelling west we know that each car going east will make another pairs.
function solution(A) {
let east = 0
let pairs = 0
for (let i = 0; i < A.length; ++i) {
if (A[i] === 0) east++
else {
pairs += east
// if we exceed the passing car count then return
if (pairs > 1000000000) return -1
}
}
return pairs
}