C++ Simple Time Complexity O(n)


#1
int Solution::solve(vector<int> &A) {
    vector<pair<int,int>> v;
    int p = 0,q = 0,x,y,cnt = 0;
    for(int i=0;i<A.size();i++)
    {
        if(i%2==0) p+=A[i];
        else q+=A[i];
        v.push_back(make_pair(p,q));
    }
    for(int i=0;i<A.size();i++)
    {
        x = v.back().first - v[i].first;
        y = v.back().second - v[i].second;
        if(i==0 && x==y) cnt++;        
        else
        {
            if(v[i-1].first + y  == v[i-1].second + x) cnt++;
        }
    }
    return cnt;
}

#3

Can you explain the logic behind it? I am finding it a bit difficult to understand.