Modulo Match in Subarrays

Count Interesting Subarrays — Modulo Frequency Check

Problem Statement

You’re given a 0-indexed integer array nums, along with two integers: modulo and k.

A subarray nums[l..r] is considered interesting if:

  • Let cnt be the number of indices i in range [l..r] such that nums[i] % modulo == k.

  • The subarray is interesting if cnt % modulo == k.

Your mission: Return the number of such interesting subarrays.

Example 1

Input:
nums = [3, 2, 4], modulo = 2, k = 1
Output:
3

Explanation:

  • Interesting subarrays:
    [3], [3,2], [3,2,4] → All have exactly 1 element satisfying num % 2 == 1.

Best Technique — Prefix Sum with HashMap Magic

This isn’t your typical sum or window problem. It’s about modulo arithmetic + prefix counts.
We care about how many indices so far have nums[i] % modulo == k.

Let’s map this into an elegant prefix sum pattern:

If prefixCount[j] - prefixCount[i] % modulo == k, then nums[i+1..j] is interesting.

Rearranged:
Let prefix[j] % modulo = currMod, we wanna count how many previous prefix values satisfy:
(currMod - k + modulo) % modulo == target

Boom, we can now use a HashMap to track frequency of prefix mods and count pairs!

Different Approaches

Approach 1: Brute Force with Modulo Count

Loop through every possible subarray and count cnt. Check if cnt % modulo == k.

public int countInterestingSubarraysBrute(int[] nums, int modulo, int k) { int count = 0; int n = nums.length; for (int i = 0; i < n; i++) { int modCount = 0; for (int j = i; j < n; j++) { if (nums[j] % modulo == k) modCount++; if (modCount % modulo == k) count++; } } return count; }

Approach 2: Optimized Using Prefix Sum + HashMap

Track the count of valid prefixSums % modulo values using a HashMap.

public long countInterestingSubarrays(List<Integer> nums, int modulo, int k) { Map<Integer, Long> modCount = new HashMap<>(); modCount.put(0, 1L); // Empty prefix counts as zero long result = 0; int prefix = 0; for (int num : nums) { if (num % modulo == k) { prefix++; } int target = (prefix - k + modulo) % modulo; result += modCount.getOrDefault(target, 0L); int currMod = prefix % modulo; modCount.put(currMod, modCount.getOrDefault(currMod, 0L) + 1); } return result; }

⚠️ Common Pitfalls

  • Forgetting to use modulo in prefix - k + modulo to avoid negative values

  • Not initializing the HashMap with base case {0: 1}

  • Mixing up num % modulo == k vs cnt % modulo == k

  • Overthinking it as a sliding window (nope—prefix counts FTW here)

📊 Time and Space Complexity

ApproachTime Complexity       Space Complexity
Brute Force      O(n²)                 O(1)
Prefix + HashMap      O(n)                 O(modulo)

🌍 Real-World Applications

  •  Monitoring patterns in event streams, like counting specific event types over time

  •  Genomic sequence analysis where certain base pair patterns follow a mod rule

  •  Data buckets where we analyze frequency-modulo patterns over a stream


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Trailing Zeros in Factorial – How Many and Why?

Divisible Drama — Who’s In, Who’s Out?

Zero Array After Queries