2845: Count-of-Interesting-Subarrays
Medium
table of contents
The key to this question is to utilise the power of prefix-sum arrays.
Example:
code
class Solution {
public:
long long countInterestingSubarrays(vector<int>& nums, int modulo, int k) {
unordered_map<int, int> mp;
mp[0] = 1; // for the cases, where the entire array is `cnt % modulo``
long long counter = 0;
long long ans = 0;
for (int i = 0; i < nums.size(); ++i) {
counter += (nums[i] % modulo == k);
ans += mp[(counter + modulo - k)%modulo];
++mp[counter%modulo];
}
return ans;
}
};