Created
April 9, 2020 14:04
-
-
Save manojnaidu619/11dbd872d34bd6a087d8dd40372485ca to your computer and use it in GitHub Desktop.
Subarray sum equals k in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
presum = {0:1} | |
cumsum, res = 0,0 | |
for x in nums: | |
cumsum += x | |
if cumsum-k in presum: | |
res += presum[cumsum-k] | |
if cumsum in presum: | |
presum[cumsum]+=1 | |
else: | |
presum[cumsum]=1 | |
print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment