The description of the code is in the following link
My Code
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
if len(nums) <2:
return False
else:
if k == 0:
InputList= nums[0:2]
if sum(InputList) == 0:
return True
else:
SumList = [[sum(InputList[i:len(InputList)]) for i in range(len(InputList))]]
SumList = [[0],[0]]+SumList # To get the save for the list with length 2
for i in range(2, len(nums)):
NewList = []
for j in range(i):
NewSum = nums[i]+SumList[i][j]
if NewSum == 0:
return True
NewList.append(NewSum)
NewList.append(nums[i])
SumList.append(NewList)
return False
else:
InputList = nums[0:2];
if sum(InputList) % k ==0:
return True
else:
SumList = [[sum(InputList[i:len(InputList)]) for i in range(len(InputList))]]
SumList = [[0],[0]]+SumList # To get the save for the list with length 2
for i in range(2, len(nums)):
NewList = []
for j in range(i):
NewSum = nums[i]+SumList[i][j]
if NewSum %k == 0:
return True
NewList.append(NewSum)
NewList.append(nums[i])
SumList.append(NewList)
return False
Explanation: The key point is still doing it brutally. We check all the possible sum and whenever we encounter find there is a full division, we return true, otherwise we return false. Here we use one extra array to improve the time complexity, otherwise it is going to be n^3, now it is n^2
Another Approach: Using Hash Table (The Solution is not clearly clarified, need to do some further reading)