Need help to understand logic in the code

The below code is returning the total number of subarrays whose sum equals to k. I am trying to understand logic in the below code: nums = [1,3,1] k = 4 dic = {0:1} res = 0 curr = 0 for i in range(len(nums)): curr += nums[i] res += dic.get(curr-k, 0) #need help to understand… Read More Need help to understand logic in the code

Why does my code to USACO Silver Breed Counting not work?

This is my code: #include <bits/stdc++.h> using namespace std; int main() { freopen("bcount.in", "r", stdin); freopen("bcount.out", "w", stdout); int n, q; cin >> n >> q; vector<int> holsteins(n); vector<int> guernseys(n); vector<int> jerseys(n); for (int i = 0 ; i < n ; i++) { holsteins[i+1]=holsteins[i]; guernseys[i+1]=guernseys[i]; jerseys[i+1]=jerseys[i]; int a; cin >> a; if (a==1) holsteins[i+1]++;… Read More Why does my code to USACO Silver Breed Counting not work?