Skip to content

Commit d6dc80a

Browse files
committed
Apr 25
1 parent 693a11b commit d6dc80a

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
def countInterestingSubarrays(self, nums: list[int], modulo: int, k: int) -> int:
3+
count, prefix = 0, 0
4+
freqs = {0: 1}
5+
for num in nums:
6+
if num % modulo == k:
7+
prefix = (prefix + 1) % modulo
8+
target = (prefix - k + modulo) % modulo
9+
count += freqs.get(target, 0)
10+
freqs[prefix] = freqs.get(prefix, 0) + 1
11+
return count
12+
13+
14+
def main():
15+
nums = [3, 2, 4]
16+
modulo = 2
17+
k = 1
18+
assert Solution().countInterestingSubarrays(nums, modulo, k) == 3
19+
20+
nums = [3, 1, 9, 6]
21+
modulo = 3
22+
k = 0
23+
assert Solution().countInterestingSubarrays(nums, modulo, k) == 2
24+
25+
26+
if __name__ == '__main__':
27+
main()

2025-04-April-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
| April 22 | [2338. Count the Number of Ideal Arrays](https://leetcode.com/problems/count-the-number-of-ideal-arrays/) | Hard | Unsolved |
2929
| April 23 | [1399. Count Largest Group](https://leetcode.com/problems/count-largest-group/) | Easy | Solved |
3030
| April 24 | [2799. Count Complete Subarrays in an Array](https://leetcode.com/problems/count-complete-subarrays-in-an-array/) | Medium | Solved |
31-
| April 25 | []() | | |
31+
| April 25 | [2845. Count of Interesting Subarrays](https://leetcode.com/problems/count-of-interesting-subarrays/) | Medium | Unsolved |
3232
| April 26 | []() | | |
3333
| April 27 | []() | | |
3434
| April 28 | []() | | |
@@ -40,5 +40,5 @@
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
4242
| Easy | 7 | 7 | 0 |
43-
| Medium | 13 | 10 | 3 |
43+
| Medium | 13 | 9 | 4 |
4444
| Hard | 4 | 0 | 4 |

0 commit comments

Comments
 (0)