subarray sum equals k

Go Back home

Time: O(n^3) 🤷
Space: O(1)

1 def subarraySum(nums, k):
2     count = 0
3     for i in range(len(nums)):
4         for j in range(i, len(nums)):
5             if sum(nums[i:j+1]) == k:
6                 count += 1
7     return count

See also:

substring search