Help me out in this problem
Yeah , what is the issue?
ok wait…
Wait ,my bad u are taking mod ,let me recheck
class Solution {
public:
int subarraysDivByK(vector<int>& nums, int k) {
map<int,int>m;
int sum=0;
int mod;
int count=0;
m[0]=1;//Initially sum is 0 so adding it to map
for(int i=0;i<nums.size();i++)
{
sum+=nums[i];
mod=(sum+100000*k)%k; //because sum can be negative
// if(nums[i]%k==0) //WHy are u even taking this ,this will add extras into answer
// {
// count++;
// }
if(m.find(mod)==m.end())
{
m[mod]=1;
}
else
{
count=count+m[mod];
m[mod]++;
}
}
return count;
}
};
If this resolves your query then please mark it as resolved
Initially sum is 0
say we get 0 sum at some index later so this is to consider subarray starting from 0th index to that index .