Short simple Explanation | C++| Java | Prefix sum
Approach:\n1. We take the sum of all elements of the array, and modulo it with P. This would be the number we need to remove in  order to make the array divisib
**Approach:**\n1. We take the sum of all elements of the array, and modulo it with P. This would be the number we ```need``` to remove in  order to make the array divisible by P.\nExample: ``` \nsuppose  we have an array, where total array sum = 50, and we want to make array divisible by P = 7. Here, the number we need to remove is 50%7=1, to make it divisible by P.```\n2. Now, the problem reduces to simply finding the minimum subarray size having sum as ```need```. (quite similar to [LC 560](https://leetcode.com/problems/subarray-sum-equals-k/)).\n3. We simply maintain a map, and search for the minimum length subarray. The code will make it more clear.\n\n**Pls Upvote if it\'s helpful : )**\nTime & Space complexity = O(N)\n* C++\n```\nclass Solution {\npublic:\n    int minSubarray(vector<int>& a, int p) \n    {\n        int need = 0;\n        int n = a.size();\n        \n        for(auto i : a)\n            need = (need + i)%p;\n        \n        if(need == 0)\n            return 0;\n        \n        unordered_map<int, int> m;\n        m[0] = -1;\n        int prefix = 0, ans = n;\n        \n        for(int i=0;i<n;i++)\n        {\n            prefix = (prefix + a[i])%p;\n            int search = (prefix - need)%p;\n            \n            if(search < 0)\n                search += p;\n            \n            if(m.count(search))\n                ans = min(ans, i-m[search]);\n            \n            m[prefix] = i;\n        }\n        \n        return ans==n ? -1 : ans;\n    }\n};\n```\n* Java\n```\nclass Solution {\n    public int minSubarray(int[] a, int p)\n    {\n        int need = 0;\n        int n = a.length;\n        \n        for(var i : a)\n            need = (need+i) % p;\n        \n        if(need == 0)\n            return 0;\n        \n        Map<Integer, Integer> m = new HashMap<>();\n        m.put(0, -1);\n        \n        int prefix = 0, ans = n;\n        for(int i=0;i<n;i++)\n        {\n            prefix = (prefix + a[i])%p;\n            \n            int search = (prefix - need)%p;\n            if(search < 0)\n                search += p;\n            \n            if(m.containsKey(search))\n            {\n                ans = Math.min(ans, i-m.get(search));\n            }\n            \n            m.put(prefix, i);\n        }\n        \n        return ans==n ? -1 : ans;\n    }\n}\n```
['Prefix Sum']