2845: Count-of-Interesting-Subarrays
Medium
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) {
<int, int> mp;
unordered_map[0] = 1; // for the cases, where the entire array is `cnt % modulo``
mplong long counter = 0;
long long ans = 0;
for (int i = 0; i < nums.size(); ++i) {
+= (nums[i] % modulo == k);
counter += mp[(counter + modulo - k)%modulo];
ans ++mp[counter%modulo];
}
return ans;
}
};