Input: nums =[3,2,4], modulo =2, k =1Output: 3Explanation: In this example the interesting subarrays are:The subarray nums[0..0] which is[3].- There is only one index, i =0,in the range [0,0] that satisfies nums[i]% modulo == k.- Hence, cnt =1 and cnt % modulo == k.The subarray nums[0..1] which is[3,2].- There is only one index, i =0,in the range [0,1] that satisfies nums[i]% modulo == k.- Hence, cnt =1 and cnt % modulo == k.The subarray nums[0..2] which is[3,2,4].- There is only one index, i =0,in the range [0,2] that satisfies nums[i]% modulo == k.- Hence, cnt =1 and cnt % modulo == k.It can be shown that there are no other interesting subarrays. So, the answer is3.
Example 2:
1
2
3
4
5
6
7
8
9
10
Input: nums =[3,1,9,6], modulo =3, k =0Output: 2Explanation: In this example the interesting subarrays are:The subarray nums[0..3] which is[3,1,9,6].- There are three indices, i =0,2,3,in the range [0,3] that satisfy nums[i]% modulo == k.- Hence, cnt =3 and cnt % modulo == k.The subarray nums[1..1] which is[1].- There is no index, i,in the range [1,1] that satisfies nums[i]% modulo == k.- Hence, cnt =0 and cnt % modulo == k.It can be shown that there are no other interesting subarrays. So, the answer is2.
To solve this problem, the goal is to efficiently determine the count of “interesting” subarrays instead of naively calculating for all possible subarrays. Using mathematical insights regarding modulo operations and prefix sums can significantly optimise the solution.
A subarray is interesting if the number of indices i within the subarray where nums[i] % modulo == k satisfies:
1
cnt % modulo == k
We’ll focus on efficiently counting these subarrays using prefix sums and hashmaps.
So, we can:
Convert each element nums[i] to 1 if nums[i] % modulo == k, otherwise 0.
Use a prefix sum array to calculate the count of such elements (as 1s) at each position.
The challenge now turns into finding valid subarrays such that: prefix[r] - prefix[l - 1] % modulo == k
classSolution {
publiclongcountInterestingSubarrays(
List<Integer> nums,
int modulo,
int k
) {
long ans = 0; // To store the final count of interesting subarraysint prefix = 0; // Prefix sum modulo counter HashMap<Integer, Integer> freq =new HashMap<>();
freq.put(0, 1); // Initial condition: one subarray starting at index 0for (int num : nums) {
// Update prefix when nums[i] % modulo == kif (num % modulo == k) {
prefix++;
}
// Compute current prefix moduloint prefixMod = prefix % modulo;
// Compute the target (satisfying condition cnt % modulo == k)int target = (prefixMod - k + modulo) % modulo;
// Add the count of this target prefix to the result ans += freq.getOrDefault(target, 0);
// Update the frequency map with the current prefix modulo freq.put(prefixMod, freq.getOrDefault(prefixMod, 0) + 1);
}
return ans;
}
}
classSolution:
defcountInterestingSubarrays(self, nums: List[int], modulo: int, k: int) -> int:
ans =0 prefix =0 freq = {0: 1}
for num in nums:
# Update prefix sum modulo `modulo`if num % modulo == k:
prefix +=1 prefix_mod = prefix % modulo
# Match condition: target = (prefix_mod - k) % modulo
# Add the frequency of target to the count ans += freq.get(target, 0)
# Update frequency map freq[prefix_mod] = freq.get(prefix_mod, 0) +1return ans