diff --git a/python/523-Continuous-Subarray-Sum.py b/python/523-Continuous-Subarray-Sum.py new file mode 100644 index 000000000..0c6daf79b --- /dev/null +++ b/python/523-Continuous-Subarray-Sum.py @@ -0,0 +1,18 @@ +#We are basically storing sum%k and storing it in the hashmap and checking it. +#Math logic is that the overall sum will get cancelled out because of modulo + +class Solution: + def checkSubarraySum(self, nums: List[int], k: int) -> bool: + hashmap = {} + hashmap[0]=-1 + summ=0 + for i,j in enumerate(nums): + summ+=j + if summ%k in hashmap.keys(): + if i-hashmap[summ%k]>=2: + return True + else: + continue + hashmap[summ%k]=i + return False +