Python3 prefix sum + dictionary - Make Sum Divisible by P
The key here is to keep a dictionary remainders whose key is prefix sum modulo p and value is the last index of such a prefix sum.\n\n\nclass Solution:\n    def
The key here is to keep a dictionary `remainders` whose key is prefix sum modulo `p` and value is the last index of such a prefix sum.\n\n```\nclass Solution:\n    def minSubarray(self, nums: List[int], p: int) -> int:\n        total = sum(nums) \n        remainder = total % p\n        if not remainder:\n            return 0\n        ans = float(\'inf\') \n        presum = [0]\n        remainders = {0: -1}\n        for i, n in enumerate(nums):\n            presum.append(presum[-1] + n)\n            r = ((cr := presum[-1] % p) - remainder) % p\n            if r in remainders:\n                ans = min(ans, i - remainders[r])\n            remainders[cr] = i\n        return ans if ans < len(nums) else -1\n```
['Python', 'Python3']