|
| 1 | +# [2845.Count of Interesting Subarrays][title] |
| 2 | + |
| 3 | +## Description |
| 4 | +You are given a **0-indexed** integer array `nums`, an integer `modulo`, and an integer `k`. |
| 5 | + |
| 6 | +Your task is to find the count of subarrays that are **interesting**. |
| 7 | + |
| 8 | +A **subarray** `nums[l..r]` is **interesting** if the following condition holds: |
| 9 | + |
| 10 | +- Let `cnt` be the number of indices `i` in the range `[l, r]` such that `nums[i] % modulo == k`. Then, `cnt % modulo == k`. |
| 11 | + |
| 12 | +Return an integer denoting the count of interesting subarrays. |
| 13 | + |
| 14 | +**Note**: A subarray is a contiguous non-empty sequence of elements within an array. |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +``` |
| 19 | +Input: nums = [3,2,4], modulo = 2, k = 1 |
| 20 | +Output: 3 |
| 21 | +Explanation: In this example the interesting subarrays are: |
| 22 | +The subarray nums[0..0] which is [3]. |
| 23 | +- There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k. |
| 24 | +- Hence, cnt = 1 and cnt % modulo == k. |
| 25 | +The subarray nums[0..1] which is [3,2]. |
| 26 | +- There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k. |
| 27 | +- Hence, cnt = 1 and cnt % modulo == k. |
| 28 | +The subarray nums[0..2] which is [3,2,4]. |
| 29 | +- There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k. |
| 30 | +- Hence, cnt = 1 and cnt % modulo == k. |
| 31 | +It can be shown that there are no other interesting subarrays. So, the answer is 3. |
| 32 | +``` |
| 33 | + |
| 34 | +**Example 2:** |
| 35 | + |
| 36 | +``` |
| 37 | +Input: nums = [3,1,9,6], modulo = 3, k = 0 |
| 38 | +Output: 2 |
| 39 | +Explanation: In this example the interesting subarrays are: |
| 40 | +The subarray nums[0..3] which is [3,1,9,6]. |
| 41 | +- There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k. |
| 42 | +- Hence, cnt = 3 and cnt % modulo == k. |
| 43 | +The subarray nums[1..1] which is [1]. |
| 44 | +- There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k. |
| 45 | +- Hence, cnt = 0 and cnt % modulo == k. |
| 46 | +It can be shown that there are no other interesting subarrays. So, the answer is 2. |
| 47 | +``` |
| 48 | + |
| 49 | +## 结语 |
| 50 | + |
| 51 | +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] |
| 52 | + |
| 53 | +[title]: https://leetcode.com/problems/count-of-interesting-subarrays |
| 54 | +[me]: https://github.com/kylesliu/awesome-golang-algorithm |
0 commit comments