Python, O(N), use hash map and prefix sum with explanation
\n# (allsum - sum1) % k == 0 ----> requirement, find a sum1 and remove it\n# sum1 = prefixsum2 - prefixsum1 ------> prefixsum2 is current sum, prefixsum1 is pre
```\n# (allsum - sum1) % k == 0 ----> requirement, find a sum1 and remove it\n# sum1 = prefixsum2 - prefixsum1 ------> prefixsum2 is current sum, prefixsum1 is previous sum\n# (allsum - (prefixsum2 - prefixsum1)) % k == 0\n# (allsum - prefixsum2) % k == -prefixsum1 % k\n# (-allsum + prefixsum2) % k == prefixsum1 % k\n# if we find a range sum whose remainder difference with allsum that has appeared before, <---- use hash map to save lookup time\n# we can remove it to make the sum of the rest is divisible by P. At the same time we are looking for the shortest subarray(range sum).\nclass Solution:\n    def minSubarray(self, nums: List[int], p: int) -> int:\n        sumMod = sum(nums) % p\n        if sumMod == 0: return 0\n        prefixsum = 0\n        remainder2idx = {0:-1}\n        minLen = len(nums)\n        for idx, n in enumerate(nums):\n            prefixsum += n\n            prefixsum %= p # (11 % p + 3) % p == (11 + 3) % p, it is remainder now\n            target = (-sumMod + prefixsum + p) % p # it is possible that -sumMod + prefixsum is less than 0, make remainder > 0\n            if target in remainder2idx:\n                minLen = min(minLen, idx - remainder2idx[target])\n            remainder2idx[prefixsum] = idx\n\n        return -1 if minLen == len(nums) else minLen\n```
['Prefix Sum', 'Python']