DETAILED EXPLANATION OF MATH BEHIND O(N) SOLUTION
If you don\'t know what the prefix-sum technique is, please first go through:\n subarray-sum-equals-k - can be thought of as an intro to prefix sum problems, an
If you don\'t know what the prefix-sum technique is, please first go through:\n* [subarray-sum-equals-k](https://leetcode.com/problems/subarray-sum-equals-k/) - can be thought of as an intro to prefix sum problems, and I have provided the same explanation for this problem [here](https://leetcode.com/problems/subarray-sum-equals-k/discuss/867435/MATH-BEHIND-PREFIX-SUM-PYTHON-EASY-TO-UNDERSTAND)\n* [subarray-sums-divisible-by-k](https://leetcode.com/problems/subarray-sums-divisible-by-k/), which can be thought of as a precursor to this problem, and I have provided a detailed explanation for this problem [here](https://leetcode.com/problems/subarray-sums-divisible-by-k/discuss/859080/DETAILED-EXPLANATION-OF-MATH-BEHIND-O(N)-SOLUTION)\n\n![image](https://assets.leetcode.com/users/images/6c05c460-119c-4dc3-bb35-2a63560b7db3_1601488876.403818.png)\n![image](https://assets.leetcode.com/users/images/b676c950-5183-4ded-9211-f9ea15b60c31_1601488879.2567034.png)\n![image](https://assets.leetcode.com/users/images/42f14a7f-ba73-48c4-822d-4d96fbcd5395_1601488883.5258496.png)\n\n\n\n\nPython3: \n```\nclass Solution:\n    def minSubarray(self, nums: List[int], p: int) -> int:\n        n = len(nums)\n        target = sum(nums)%p\n        if not target:\n            return 0\n        answer = n\n        prefix_sum = 0\n        hashmap = {0: -1}\n        for i, num in enumerate(nums):\n            prefix_sum += num\n            key = (prefix_sum%p - target)%p\n            if key in hashmap:\n                answer = min(answer, i-hashmap[key])\n            hashmap[prefix_sum%p] = i\n        return answer if answer < n else -1\n```
[]