Solution with Comments for each step
\nclass Solution {\npublic:\n    int minSubarray(vector<int>& nums, int p) {\n        int n = nums.size();\n        int mod = 0; //stores total sum%p \n        
```\nclass Solution {\npublic:\n    int minSubarray(vector<int>& nums, int p) {\n        int n = nums.size();\n        int mod = 0; //stores total sum%p \n        for(int i=0; i<n;++i)\n        {\n            mod = (mod+nums[i])%p; //stores total sum%p \n        }\n        if(!mod)return 0; //if entire array is divisible\n        int sum = 0; //will store currsum\'s % \n        unordered_map<int, int> m;\n        int ans = n;\n        m[0] = -1; //no elements mean no mod\n        for(int i=0; i<n; ++i)\n        {\n            sum = (sum+nums[i])%p; //mod until current sum\n            int check = (sum - mod + p)%p; \n//Tricky step: Basically we want subarray s.t. totalsum%p == subarraysum%p\n//so sum gives us subarray%p now we compare it with the mod of total array and subtract it\n//we have added p here to avoid negatives\n//if this value has previously been found in the array that means effectively \n// subarraysum%p - totalsum%p == 0 in this particular range so we found the answer \n//why? rearrange it to realise subarraysum%p ==  totalsum%p \n//the last statement can be understood from the question subarray sum equal to k\n            if(m.find(check)!=m.end())\n            {\n                ans = min(ans, i - m[check]);\n            }\n            m[sum] = i;\n//we add the current subsum%p to the map\n        }\n//if ans is ever found we take it or we return -1\n        return ans<n? ans :-1;\n    }\n};\n```
[]