Skip to content

Commit a39c539

Browse files
authored
Merge pull request #71 from InflixOP/patch1
Create #2845. Count of Interesting Subarrays.cpp
2 parents 25ee06a + 4418fbd commit a39c539

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
long long countInterestingSubarrays(vector<int>& nums, int mod, int k) {
4+
int n=nums.size();
5+
vector<int>pref(n,0);
6+
map<int,int>mp;
7+
int cnt=0;
8+
long long ans=0;
9+
mp[0]=1;
10+
for(int i=0;i<n;i++){
11+
pref[i]=(nums[i]%mod==k)+cnt;
12+
int l=pref[i]%mod;
13+
if(mp.find((l-k+mod)%mod)!=mp.end()){
14+
ans+=mp[(l-k+mod)%mod];
15+
}
16+
cnt=pref[i];
17+
mp[l]++;
18+
}
19+
return ans;
20+
}
21+
};

0 commit comments

Comments
 (0)