Leetcode 1694 Solution

This article provides solution to leetcode question 1694 (make-sum-divisible-by-p)

https://leetcode.com/problems/make-sum-divisible-by-p

Solution

class Solution:
    def minSubarray(self, nums: List[int], p: int) -> int:
        target = sum(nums) % p

        if target == 0:
            return 0

        m = {0: -1}
        s = 0
        ans = len(nums)
        for i, num in enumerate(nums):
            s += num

            if (s - target + p) % p in m:
                ans = min(ans, i - m[(s - target + p) % p])

            m[s % p] = i

        return ans if ans != len(nums) else -1