Help me out in this problem


@KartikKhariwal are u there ?

Yeah , what is the issue?

@Kartikkhariwal1 the 43 testcase are passing can you check why?

ok wait…

Wait ,my bad u are taking mod ,let me recheck :slight_smile:

@Kartikkhariwal1 i have considered that case kindly read the question

Hey @shivammishra20121999

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 :slight_smile:

@Kartikkhariwal1 m[0]=1 can you tell why you took this?

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 .

@KartikKhariwal now a days i am doing lot of silly mistakes
thks for help