PassingCars

Count the number of passing cars on the road.

Solution in C/C++ (easy to convert into other languages...)

int solution(vector<int> &A) {     unsigned long sum = 0;     unsigned long sum_1 = 0;     for (auto it = A.rbegin(); it != A.rend(); ++it) {         if (*it == 1) {             ++sum_1;         }         else {             sum += sum_1;         }         if (sum>1000000000)             return -1;     }     return sum; }
Comments