2845. Count of Interesting Subarrays
Difficulty: Medium
Topics: Array
, Hash Table
, Prefix Sum
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 indicesi
in the range[l, r]
such thatnums[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.
- In this example the interesting subarrays are:
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.
- In this example the interesting subarrays are:
Constraints:
-
1 <= nums.length <= 105
1 <= nums[i] <= 109
1 <= modulo <= 109
0 <= k < modulo
Hint:
- The problem can be solved using prefix sums.
- Let
count[i]
be the number of indices wherenums[i] % modulo == k
among the firsti
indices. -
count[0] = 0
andcount[i] = count[i - 1] + (nums[i - 1] % modulo == k ? 1 : 0)
fori = 1, 2, ..., n
. - Now we want to calculate for each
i = 1, 2, ..., n
, how many indicesj < i
such that(count[i] - count[j]) % modulo == k
. - Rewriting
(count[i] - count[j]) % modulo == k
becomescount[j] = (count[i] + modulo - k) % modulo
. - Using a map data structure, for each
i = 0, 1, 2, ..., n
, we just sum up allmap[(count[i] + modulo - k) % modulo]
before increasingmap[count[i] % modulo]
, and the total sum is the final answer.
Solution:
We need to count the number of interesting subarrays in a given array nums
based on specific conditions. A subarray is considered interesting if the count of elements in it that satisfy a certain modulo condition, when taken modulo modulo
, equals k
.
Approach
- Prefix Sum with Modulo Handling: We use a prefix sum approach to keep track of the count of elements that meet the modulo condition as we iterate through the array.
-
Hash Map for Efficient Lookup: We maintain a hash map to store the frequency of each prefix sum modulo
modulo
. This allows us to efficiently check how many times a specific value has been encountered, which helps in determining the number of interesting subarrays ending at each position.
Let's implement this solution in PHP: 2845. Count of Interesting Subarrays
/**
* @param Integer[] $nums
* @param Integer $modulo
* @param Integer $k
* @return Integer
*/
function countInterestingSubarrays($nums, $modulo, $k) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example usage:
$nums1 = array(3, 2, 4);
$modulo1 = 2;
$k1 = 1;
echo countInterestingSubarrays($nums1, $modulo1, $k1) . "\n"; // Output: 3
$nums2 = array(3, 1, 9, 6);
$modulo2 = 3;
$k2 = 0;
echo countInterestingSubarrays($nums2, $modulo2, $k2) . "\n"; // Output: 2
?>
Explanation:
-
Initialization: We start with a hash map initialized to
{0: 1}
to account for the prefix sum of 0 before any elements are processed. -
Iterating through Elements: For each element in the array, we check if it meets the modulo condition
nums[i] % modulo == k
and update the current count of such elements. -
Target Calculation: For each element, we compute the target value, which is the modulo-adjusted value of the current count minus
k
. This target helps us find how many previous prefix sums would form an interesting subarray ending at the current element. - Result Update: We look up the target value in the hash map and add its frequency to the result. This gives the number of valid subarrays ending at the current position.
-
Updating Hash Map: After processing each element, we update the hash map with the current prefix sum modulo
modulo
to include the current count in future lookups.
This approach efficiently counts the interesting subarrays in linear time, making it suitable for large input sizes as specified in the problem constraints.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me: