C++ / O(n)    Easy Solution with Explaination
Approach:\nFind out the sum of array elements and what is the remainder that you are getting (say need).\nSo we just need to eliminate the subarray (minimum) wi
**Approach:**\nFind out the sum of array elements and what is the remainder that you are getting (say need).\nSo we just need to eliminate the subarray (minimum) with remainder which is equal to \'need\'.\nFor that, while traversing the array maintain an unordered_map (or Hashmap) which stores the recent position of the particular remainder that has been encountered and keep track of the minimum required subarray ( with remainder = need).\n\n(Feel free to drop a comment )\n\n**CODE:** \n```\nclass Solution {\npublic:\n    int minSubarray(vector<int>& nums, int p) {\n        unordered_map<int,int> m;\n        long long sum = 0;\n        for(long long i =0 ;i < nums.size(); i++) sum+=nums[i];\n        long long need = sum%p;\n        if(need==0) return 0;\n\t\t\n        long long x = nums[0];\n        m[x%p] = 0;\n        \n\t\tlong long ans = INT_MAX;\n        for(long long i = 1; i < nums.size(); i++){\n            x+=nums[i];\n\t\t\t\n            if(x%p==need && i!=nums.size()-1) ans = min(ans, i+1);\n            long long key = ((x%p) - need + p)%p;\n            if(m.find(key)!=m.end()){ \n                long long tmp = i - m[key];\n                ans = min(ans, tmp);\n            }\n            m[x%p] = i;\n        }\n        if(ans==INT_MAX) return -1;\n        return ans;\n    }\n};\n```
[]