Skip to content

Commit 2c95b29

Browse files
authored
Create Solution.rs
1 parent 58c361f commit 2c95b29

File tree

1 file changed

+24
-0
lines changed
  • solution/2800-2899/2845.Count of Interesting Subarrays

1 file changed

+24
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::collections::HashMap;
2+
3+
impl Solution {
4+
pub fn count_interesting_subarrays(nums: Vec<i32>, modulo: i32, k: i32) -> i64 {
5+
let mut arr: Vec<i32> = nums
6+
.iter()
7+
.map(|&x| if x % modulo == k { 1 } else { 0 })
8+
.collect();
9+
let mut cnt: HashMap<i32, i64> = HashMap::new();
10+
cnt.insert(0, 1);
11+
12+
let mut ans: i64 = 0;
13+
let mut s: i32 = 0;
14+
15+
for x in arr {
16+
s += x;
17+
let key = (s - k).rem_euclid(modulo);
18+
ans += *cnt.get(&key).unwrap_or(&0);
19+
*cnt.entry(s % modulo).or_insert(0) += 1;
20+
}
21+
22+
ans
23+
}
24+
}

0 commit comments

Comments
 (0)