PassingCars

PHOTO EMBED

Tue Jun 21 2022 21:17:57 GMT+0000 (Coordinated Universal Time)

Saved by @jacobsfo

// you can use includes, for example:
// #include <algorithm>

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;

int solution(vector<int> &A) {
    // write your code in C++14 (g++ 6.2.0)
    //counter for Se
    int se=0;
    //counter for w
    long long int s = 0;
//loop
for(int i = 0; i < A.size();i++ )
{
    //if element =0 se++
    if(A[i]==0){se++;} 
    //if element =1 add both se and s and this gives us passing cars(how many se go with s)
    if(A[i]==1){s=s+se;}
   
}
//edge case
if(s>1000000000){return -1;}
return s;
    

}
content_copyCOPY