3
int subarraysDivByK(vector<int> &nums, int k) {4
// take an ans variable6
// initialize a map of int, int and insert {0,1} as 0 occurs first time for8
unordered_map<int, int> mapp;10
// initialize presum = 0 and remainder rem = 0 which will be used in further18
1. We will traverse the entire given array/vector.19
2. While traversing we will add the element in our presum, i.e presum +=21
3. Now we will do the % of presum and k and store it in rem that we have23
4. We need to take care of negative value of rem. If it is < 0, then we24
will add k to the remainder to make it positive.25
5. Now we will check if rem already exist in the map. If it exist then26
we will add it's frequency to ans variable and then increment rem's value27
in map, i.e. mapp[rem]++, else we will add it in the map.28
6. At last we will return ans.31
for (int i = 0; i < nums.size(); i++) {34
if (rem < 0) rem += k;36
if (mapp.find(rem) != mapp.end()) {40
mapp.insert({rem, 1});