Map || Prefix Sum || C++
Intuition\n Describe your first thoughts on how to solve this problem. \n\nSimilar to LettCode 974 : https://leetcode.com/problems/subarray-sums-divisible-by-k/
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nSimilar to LettCode 974 : https://leetcode.com/problems/subarray-sums-divisible-by-k/\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nFirst calculate the remainder when total sum is devided by p , let say x.\nThat subarray with remainder x we have to remove. Now we need to find the smallest subarray with remainder equals to x . Same idea for #974 need to be applied here .\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int minSubarray(vector<int>& a, int p) {\n        \n        int n=a.size();\n        vector<long long> ps(n);\n        ps[0]=a[0];\n        long long s=a[0];\n        for(int i=1;i<n;i++)\n        {\n            s+=a[i];\n            ps[i]=a[i]+ps[i-1];\n            if((i-3)>=0)\n             cout<<ps[i]-ps[i-3]<<" ";\n        }\n        cout<<endl;\n        cout<<"sum is :: "<<s<<endl;\n        cout<<"rem is :: "<<s%p<<endl;\n         \n        if((s%p)==0)\n         return 0;\n        int x=s%p;\n        unordered_map<long long,int> m;\n        long long sum=0;\n        int ans=1e6;\n        m[0]=-1;\n        for(int i=0;i<n;i++)\n        {\n            sum+=a[i];\n            int rem=sum%p;\n            int k=(rem-x+p)%p;\n            if(m.find(k)!=m.end())\n            {  \n                ans=min(ans,i-m[k]);\n                m[rem]=i;\n            }\n            else\n             m[rem]=i;\n        }\n\n        if(ans==1e6 || ans==n)\n         ans=-1;\n        return ans;\n\n    }\n};\n```
['C++']