2845. Count of Interesting Subarrays Medium
1/**
2 * [2845] Count of Interesting Subarrays
3 *
4 * You are given a 0-indexed integer array nums, an integer modulo, and an integer k.
5 * Your task is to find the count of subarrays that are interesting.
6 * A subarray nums[l..r] is interesting if the following condition holds:
7 *
8 * Let cnt be the number of indices i in the range [l, r] such that nums[i] % modulo == k. Then, cnt % modulo == k.
9 *
10 * Return an integer denoting the count of interesting subarrays.
11 * <span>Note: A subarray is a contiguous non-empty sequence of elements within an array.</span>
12 *
13 * <strong class="example">Example 1:
14 *
15 * Input: nums = [3,2,4], modulo = 2, k = 1
16 * Output: 3
17 * Explanation: In this example the interesting subarrays are:
18 * The subarray nums[0..0] which is [3].
19 * - There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k.
20 * - Hence, cnt = 1 and cnt % modulo == k.
21 * The subarray nums[0..1] which is [3,2].
22 * - There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k.
23 * - Hence, cnt = 1 and cnt % modulo == k.
24 * The subarray nums[0..2] which is [3,2,4].
25 * - There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k.
26 * - Hence, cnt = 1 and cnt % modulo == k.
27 * It can be shown that there are no other interesting subarrays. So, the answer is 3.
28 * <strong class="example">Example 2:
29 *
30 * Input: nums = [3,1,9,6], modulo = 3, k = 0
31 * Output: 2
32 * Explanation: In this example the interesting subarrays are:
33 * The subarray nums[0..3] which is [3,1,9,6].
34 * - There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k.
35 * - Hence, cnt = 3 and cnt % modulo == k.
36 * The subarray nums[1..1] which is [1].
37 * - There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k.
38 * - Hence, cnt = 0 and cnt % modulo == k.
39 * It can be shown that there are no other interesting subarrays. So, the answer is 2.
40 *
41 * Constraints:
42 *
43 * 1 <= nums.length <= 10^5
44 * 1 <= nums[i] <= 10^9
45 * 1 <= modulo <= 10^9
46 * 0 <= k < modulo
47 *
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/count-of-interesting-subarrays/
52// discuss: https://leetcode.com/problems/count-of-interesting-subarrays/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56impl Solution {
57 pub fn count_interesting_subarrays(nums: Vec<i32>, modulo: i32, k: i32) -> i64 {
58
59 }
60}
61
62// submission codes end
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn test_2845() {
70 }
71}
72
Back
© 2025 bowen.ge All Rights Reserved.