## Birthday Chocolate

Solutions The solution to this problem when d=1 can be used to solve Picking Numbers as well.
A linear solution consists in:
computing the prefix sum of the input array keeping a window of size m counting how many times the window bounds differ by d Here is a solution in C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 int n, d, m; cin >> n; vector<int> v(n+1); copy_n(istream_iterator<int>(cin), n, next(begin(v))); cin >> d >> m; partial_sum(begin(v), end(v), begin(v)); auto cnt = 0; for (auto i=0; i<n; ++i) { cnt += v[i+m] - v[i] == d; } cout << cnt; The loop can be replaced with zip | map | reduce combination of patterns:
[Read More]