# 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?

# Why is this code failing a test case [Max Distance]

Problem: Given an array A of integers, find the maximum of j – i subjected to the constraint of A[i] <= A[j]. If there is no solution possible, return -1. Example : A : [3 5 4 2] Output : 2 for the pair (3, 4) INPUT: 9 8 7 -9 -1 EXPECTED OUTPUT: 1… Read More Why is this code failing a test case [Max Distance]