C++ - Map for tracking last pos for prefix sum%p
\n# Code\n\nclass Solution {\npublic:\n    int minSubarray(vector<int>& nums, int p) {\n        unordered_map<int,int> lastPos;\n        lastPos[0]=-1;\n       
\n# Code\n```\nclass Solution {\npublic:\n    int minSubarray(vector<int>& nums, int p) {\n        unordered_map<int,int> lastPos;\n        lastPos[0]=-1;\n        long long ps=0,s=0;\n        for (int i=0;i<nums.size();i++) s+=nums[i];\n        \n        int m=s%p,ans=nums.size();\n        if (m==0) return 0;\n        for (int i=0;i<nums.size();i++){\n            ps+=nums[i];\n            if (ps-m>=0 && lastPos.find((ps-m)%p)!=lastPos.end()){\n                ans = min(ans,i-lastPos[(ps-m)%p]);\n            }\n            lastPos[ps%p]=i;\n        }\n              \n        return (ans==nums.size()?-1:ans);\n    }\n};\n```
['Ordered Map', 'Prefix Sum', 'C++']