Skip to content

Commit

Permalink
Create 523-Continuous-Subarray-Sum.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SharmaTushar1 authored Oct 26, 2022
1 parent e4e905b commit fe1fd76
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions python/523-Continuous-Subarray-Sum.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fe1fd76

Please sign in to comment.