Subarray Division 2

  • + 0 comments

    o(n), assertions, performance, c++))

    int birthday(vector<int> s, int d, int m) {
        assert(s.size() > 0 && d > 0 && m > 0);
        int sum = accumulate(s.cbegin(), s.cbegin() + m, 0);
        int r = 0;
        
        for (int i = 0, e = s.size() - m; i < e; ++i)
        {
            if (sum == d)
            {
                ++r;
            }
            
            sum += s[i + m];
            sum -= s[i];
        }
        
        if (sum == d)
        {
            ++r;
        }
        
        return r;
    }