Sub Array Sum Divisible by K
Problem
https://leetcode.com/problems/subarray-sums-divisible-by-k/

Understanding of problem
In the above example, we can see 4 is occurring multiple times. Let's check if the subarray sum where prefix mod equals 4 will be divisible by zero or not.
The algorithm counts if the given subarray is divisible by "k" first
Then proceeds to update the frequency of the mod in the frequency map next
We initialise the sum to zero, so mod of zero is zero, So we add the frequency of 1 to the map
At index 0,
If we see the subarray sum at index 1, the mod value is 4 again. So we increment the count by number of time we have seen mod value as 4 so far. So far frequency of mod value 4 is "1", so count is "1"
We take sub-array of modArray[0, i] where i is 1, the mod value is 4. Then if take the sub array
The subarray sum of prefix[0,1] is 5, so it is divisible by 5, so the count is 1
If we keep moving forward to index 2
The subarray sum of prefix[0,2] is 5, so it is divisible by 5, so the count is 1
Solution
Last updated