Leetcode 523 Solution

This article provides solution to leetcode question 523 (continuous-subarray-sum)

https://leetcode.com/problems/continuous-subarray-sum

Solution

class Solution:
    def checkSubarraySum(self, nums: List[int], k: int) -> bool:
        a = collections.defaultdict(int)
        a[0] = -1

        s = 0
        for i in range(len(nums)):
            s += nums[i]

            if s % k in a:
                if i - a[s % k] >= 2:
                    return True

            if s % k not in a:
                a[s % k] = i

        return False