Count of Interesting Subarrays - Problem

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:

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

In other words, we need subarrays where the count of elements satisfying the modulo condition also satisfies the same modulo condition!

Return the total number of such interesting subarrays.

Note: A subarray is a contiguous non-empty sequence of elements within an array.

Input & Output

example_1.py โ€” Basic case
$ Input: nums = [3,2,4], modulo = 2, k = 1
โ€บ Output: 3
๐Ÿ’ก Note: Special elements are those where nums[i] % 2 == 1, so [3,4] are special. Subarrays with count % 2 == 1: [3] (1 special), [2,4] (1 special), [3,2,4] (2 special elements, but 2%2โ‰ 1), so actually [3], [4], [3,2,4] won't work. Let me recalculate: [3] has 1 special, 1%2=1 โœ“, [2] has 0 special, 0%2โ‰ 1, [4] has 1 special, 1%2=1 โœ“, [3,2] has 1 special, 1%2=1 โœ“. Answer is 3.
example_2.py โ€” All elements special
$ Input: nums = [3,1,9,6], modulo = 3, k = 0
โ€บ Output: 6
๐Ÿ’ก Note: Special elements are [3,9,6] (indices 0,2,3). We want subarrays where count of special elements % 3 == 0. Subarrays with 0 special: [1]. Subarrays with 3 special: [3,1,9,6]. Total valid subarrays: 6.
example_3.py โ€” No special elements
$ Input: nums = [2,4], modulo = 3, k = 1
โ€บ Output: 0
๐Ÿ’ก Note: No elements satisfy nums[i] % 3 == 1, so all subarrays have 0 special elements. Since 0 % 3 โ‰  1, no subarrays are interesting.

Visualization

Tap to expand
Magic Ledger System3169Remainder LedgerR=0: โ– โ–  (count: 2)R=1: โ–  (count: 1)R=2: โ–  (count: 1)Green = Special gemsYellow = Regular gemsmodulo = 3, k = 0
Understanding the Visualization
1
Setup Magic Ledger
Initialize with empty chest count {remainder_0: 1}
2
Process Each Gem
Check if gem is special, update running count
3
Calculate Remainders
Find what previous remainder would give us target count
4
Count Matches
Look up matching chests in our ledger
5
Update Ledger
Record current chest configuration
Key Takeaway
๐ŸŽฏ Key Insight: By tracking prefix remainder frequencies, we can count valid subarrays in O(n) time instead of checking all O(nยฒ) possibilities!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through array, hash map operations are O(1) on average

n
2n
โœ“ Linear Growth
Space Complexity
O(min(n, modulo))

Hash map stores at most modulo different remainders

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • 1 โ‰ค modulo โ‰ค 109
  • 0 โ‰ค k < modulo
Asked in
Google 25 Amazon 18 Meta 15 Microsoft 12
28.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen