Solution By Dare2Solve | Detailed Explanation | Clean Code
Explanation []\nauthorslog.com/blog/SUUrudMJGl\n\n# Code\n\ncpp []\nclass Solution {\npublic:\n    int minSubarray(vector<int>& nums, int p) {\n        long lon
```Explanation []\nauthorslog.com/blog/SUUrudMJGl\n```\n# Code\n\n```cpp []\nclass Solution {\npublic:\n    int minSubarray(vector<int>& nums, int p) {\n        long long totalSum = accumulate(nums.begin(), nums.end(), 0LL);  // Use 0LL to force long long\n\n        int remainder = totalSum % p;\n\n        if (remainder == 0)\n            return 0;\n\n        int n = nums.size();\n        int minLength = n;\n        int prefixSum = 0;\n        unordered_map<int, int> prefixSums;\n\n        prefixSums[0] = -1;\n\n        for (int i = 0; i < n; i++) {\n            prefixSum = (prefixSum + nums[i]) % p;\n            int target = (prefixSum - remainder + p) % p;\n\n            if (prefixSums.find(target) != prefixSums.end()) {\n                minLength = min(minLength, i - prefixSums[target]);\n            }\n\n            prefixSums[prefixSum] = i;\n        }\n\n        return minLength == n ? -1 : minLength;\n    }\n};\n```\n\n```python []\nclass Solution:\n    def minSubarray(self, nums, p):\n        totalSum = sum(nums)\n        remainder = totalSum % p\n\n        if remainder == 0:\n            return 0\n\n        n = len(nums)\n        minLength = n\n        prefixSum = 0\n        prefixSums = {0: -1}\n\n        for i in range(n):\n            prefixSum = (prefixSum + nums[i]) % p\n            target = (prefixSum - remainder + p) % p\n\n            if target in prefixSums:\n                minLength = min(minLength, i - prefixSums[target])\n\n            prefixSums[prefixSum] = i\n\n        return minLength if minLength < n else -1\n\n```\n\n```java []\npublic class Solution {\n    public int minSubarray(int[] nums, int p) {\n        long totalSum = 0;\n        for (int num : nums) {\n            totalSum += num;\n        }\n\n        int remainder = (int)(totalSum % p);\n        if (remainder == 0) return 0;\n\n        HashMap<Integer, Integer> prefixSums = new HashMap<>();\n        prefixSums.put(0, -1);\n\n        int n = nums.length;\n        int prefixSum = 0;\n        int minLength = n;\n\n        for (int i = 0; i < n; i++) {\n            prefixSum = (prefixSum + nums[i]) % p;\n            if (prefixSum < 0) {\n                prefixSum += p;  // Adjust for negative remainders\n            }\n\n            int target = (prefixSum - remainder + p) % p;\n            if (prefixSums.containsKey(target)) {\n                minLength = Math.min(minLength, i - prefixSums.get(target));\n            }\n\n            prefixSums.put(prefixSum, i);\n        }\n\n        return minLength == n ? -1 : minLength;\n    }\n}\n```\n\n```javascript []\nfunction minSubarray(nums, p) {\n    const totalSum = nums.reduce((acc, num) => acc + num, 0);\n    const remainder = totalSum % p;\n\n    if (remainder === 0) return 0;\n\n    const n = nums.length;\n    let minLength = n;\n    let prefixSum = 0;\n    const prefixSums = new Map();\n\n    prefixSums.set(0, -1);\n\n    for (let i = 0; i < n; i++) {\n        prefixSum = (prefixSum + nums[i]) % p;\n        const target = (prefixSum - remainder + p) % p;\n\n        if (prefixSums.has(target)) {\n            minLength = Math.min(minLength, i - prefixSums.get(target));\n        }\n\n        prefixSums.set(prefixSum, i);\n    }\n\n    return minLength === n ? -1 : minLength;\n}\n```
['Hash Table', 'Prefix Sum', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']