-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.py
More file actions
49 lines (39 loc) · 1.37 KB
/
Solution.py
File metadata and controls
49 lines (39 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from typing import List
class Solution:
def checkSubarraySum(self, nums: List[int], k: int) -> bool:
# unordered map 'um' implemented
# as hash table
um = {}
n = len(nums)
# 'mod_arr[i]' stores (sum[0..i] % k)
mod_arr = [0 for i in range(n)]
max_len = 0
curr_sum = 0
# Traverse arr[] and build up
# the array 'mod_arr[]'
for i in range(n):
curr_sum += arr[i]
# As the sum can be negative,
# taking modulo twice
mod_arr[i] = ((curr_sum % k) + k) % k
# If true then sum(0..i) is
# divisible by k
if (mod_arr[i] == 0):
# Update 'max_len'
max_len = i + 1
# If value 'mod_arr[i]' not present in
# 'um' then store it in 'um' with index
# of its first occurrence
elif (mod_arr[i] not in um):
um[mod_arr[i]] = i
else:
# If true, then update 'max_len'
if (max_len < (i - um[mod_arr[i]])):
max_len = i - um[mod_arr[i]]
# Return the required length of longest subarray
# with sum divisible by 'k'
return max_len
nums = [23, 2, 6, 4, 7]
k = 13
sol1 = Solution()
print(sol1.checkSubarraySum(nums, k))