Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed integer array nums, an integer modulo, and an integer k.
Your task is to find the count of subarrays that are interesting.
A subarray nums[l..r] is interesting if the following condition holds:
cnt be the number of indices i in the range [l, r] such that nums[i] % modulo == k. Then, cnt % modulo == k.Return an integer denoting the count of interesting subarrays.
Note: A subarray is a contiguous non-empty sequence of elements within an array.
Example 1:
Input: nums = [3,2,4], modulo = 2, k = 1 Output: 3 Explanation: 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 is 3.
Example 2:
Input: nums = [3,1,9,6], modulo = 3, k = 0 Output: 2 Explanation: 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 is 2.
Constraints:
1 <= nums.length <= 105 1 <= nums[i] <= 1091 <= modulo <= 1090 <= k < moduloProblem summary: You are given a 0-indexed integer array nums, an integer modulo, and an integer k. Your task is to find the count of subarrays that are interesting. A subarray nums[l..r] is interesting if the following condition holds: Let cnt be the number of indices i in the range [l, r] such that nums[i] % modulo == k. Then, cnt % modulo == k. Return an integer denoting the count of interesting subarrays. Note: A subarray is a contiguous non-empty sequence of elements within an array.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[3,2,4] 2 1
[3,1,9,6] 3 0
subarray-sums-divisible-by-k)count-number-of-nice-subarrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2845: Count of Interesting Subarrays
class Solution {
public long countInterestingSubarrays(List<Integer> nums, int modulo, int k) {
int n = nums.size();
int[] arr = new int[n];
for (int i = 0; i < n; ++i) {
arr[i] = nums.get(i) % modulo == k ? 1 : 0;
}
Map<Integer, Integer> cnt = new HashMap<>();
cnt.put(0, 1);
long ans = 0;
int s = 0;
for (int x : arr) {
s += x;
ans += cnt.getOrDefault((s - k + modulo) % modulo, 0);
cnt.merge(s % modulo, 1, Integer::sum);
}
return ans;
}
}
// Accepted solution for LeetCode #2845: Count of Interesting Subarrays
func countInterestingSubarrays(nums []int, modulo int, k int) (ans int64) {
arr := make([]int, len(nums))
for i, x := range nums {
if x%modulo == k {
arr[i] = 1
}
}
cnt := map[int]int{}
cnt[0] = 1
s := 0
for _, x := range arr {
s += x
ans += int64(cnt[(s-k+modulo)%modulo])
cnt[s%modulo]++
}
return
}
# Accepted solution for LeetCode #2845: Count of Interesting Subarrays
class Solution:
def countInterestingSubarrays(self, nums: List[int], modulo: int, k: int) -> int:
arr = [int(x % modulo == k) for x in nums]
cnt = Counter()
cnt[0] = 1
ans = s = 0
for x in arr:
s += x
ans += cnt[(s - k) % modulo]
cnt[s % modulo] += 1
return ans
// Accepted solution for LeetCode #2845: Count of Interesting Subarrays
use std::collections::HashMap;
impl Solution {
pub fn count_interesting_subarrays(nums: Vec<i32>, modulo: i32, k: i32) -> i64 {
let mut arr: Vec<i32> = nums
.iter()
.map(|&x| if x % modulo == k { 1 } else { 0 })
.collect();
let mut cnt: HashMap<i32, i64> = HashMap::new();
cnt.insert(0, 1);
let mut ans: i64 = 0;
let mut s: i32 = 0;
for x in arr {
s += x;
let key = (s - k).rem_euclid(modulo);
ans += *cnt.get(&key).unwrap_or(&0);
*cnt.entry(s % modulo).or_insert(0) += 1;
}
ans
}
}
// Accepted solution for LeetCode #2845: Count of Interesting Subarrays
function countInterestingSubarrays(nums: number[], modulo: number, k: number): number {
const arr: number[] = [];
for (const x of nums) {
arr.push(x % modulo === k ? 1 : 0);
}
const cnt: Map<number, number> = new Map();
cnt.set(0, 1);
let ans = 0;
let s = 0;
for (const x of arr) {
s += x;
ans += cnt.get((s - k + modulo) % modulo) || 0;
cnt.set(s % modulo, (cnt.get(s % modulo) || 0) + 1);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.