/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var subarraysDivByK = function(nums, k) {
let prefixMod = 0;
let result = 0;
const modGroups = new Array(k).fill(0);
modGroups[0] = 1;
for (const num of nums) {
// Take modulo twice to avoid negative remainders.
prefixMod = (prefixMod + num % k + k) % k;
result += modGroups[prefixMod];
modGroups[prefixMod]++;
}
return result;
}
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)