You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The idea here is if two sums have the same module and they are atleast two indexes apart then we can return true otherwise false.
27
-
For [0,2], k=2 prefix sum would be [0,2] and sum2 = 2 and sum1 = 0. sum2 - sum1 can be divided by k and the distance between those two indexes is atleast 2.
28
-
"""
29
-
hmap= {0:-1} # initially at index -1 the sum would be zero.
30
-
foriinrange(1,len(nums)):
31
-
nums[i] +=nums[i-1] # prefix sum
32
-
foriinrange(len(nums)):
33
-
temp=nums[i]
34
-
ifk:
35
-
temp%=k
36
-
iftempinhmap.keys():
37
-
ifi-hmap[temp] >=2:
38
-
returnTrue
25
+
hmap,total= {},0
26
+
fori,numinenumerate(nums):
27
+
total= (total+num) %k
28
+
iftotal==0andi>0: returnTrue# if total % k =0 that means we have a multiple of k present already and check if the array size is greater than or equal to 2.
0 commit comments